【问题标题】:In and Out in C# Console Application在 C# 控制台应用程序中进出
【发布时间】:2018-10-24 12:39:22
【问题描述】:

如果我输入“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();
                    }
            }
        }
    }
}

【问题讨论】:

  • 欢迎来到 SO!您的问题不是很清楚,能否请您重新表述一下您对代码的期望?
  • 嘿,Carl,仔细阅读你的代码行,你会看到你读到了一段代码,然后你反复询问他们输入的数字是否是你放入名为 num 的数组中的 3 个数字之一。如果是,您要求他们输入“In”作为 en1,但如果这不等于所采用的代码,那么它什么也不做。这真的是您想要的吗?
  • 正如其他人所说,如果你能澄清你想要做什么,我们中的一个人很乐意提供帮助。
  • 您可能不希望有递归函数 - 不需要调用 Main。

标签: c# console-application


【解决方案1】:

尽量保持简单易懂,如果您对任何部分感到困惑,请询问

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"); 
                }
            }

        }

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多