在C#控制台应用程序中进出

时间:2018-10-24 12:39:23

标签: c# console-application

如果我输入“ 01”,它将输出“ IN”,如果我再次输入“ 01”,它将消失,但问题是我需要先输入输入的第一个数字,然后才能输入另一个数字。有人能帮我吗????

class Program
{

    static string [] num = {"01","02","03"};
    static string en;
    static string en1;
    static void Main()
    {
        while (true)
            {
        Console.Clear();
        Console.Write("Enter your code: ");
        en = Console.ReadLine();
        for (int i = 0; i < num.Length; i++)
            if (en == num[i])
            {
                Console.Write("In");
                Console.ReadLine();
                Console.Clear();
                Console.Write("Enter your code: ");
                en1 = Console.ReadLine();
                    if (en1 == en)
                    {
                        Console.Write("Out");
                        Console.ReadLine();
                        Main();
                    }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

试图简化操作,如果您感到困惑,请询问

class Program
{
    static List<string> num =  new List<string> (){ "01", "02", "03" }; 
    static string en;
    static string en1;
    static void Main()
    {
        while (true) {

            Console.Write("Enter your code: ");

            if (String.IsNullOrEmpty(en)) { //check en isn't set yet
                en = Console.ReadLine(); // set en 
                if (num.Contains(en)){ // if en exists in the num list proceed
                    Console.WriteLine("IN");
                } else {
                    en = null; //if it doesn't null it and start again
                }
            } else {
                en1 = Console.ReadLine(); //read in the value to compare
                if (en == en1) { //compare the original input to this
                    en = null; //if they're the same, null the original 
                    Console.WriteLine("OUT"); 
                }
            }

        }

    }
}