【问题标题】:while loop repeating user inputs C#while循环重复用户输入C#
【发布时间】:2014-09-15 04:43:24
【问题描述】:

在我的 while 循环中,它提示用户输入一个值,一旦输入值,就会运行某些方法。我遇到的问题是提示用户输入两次值(即要求输入 A、B 或 X。用户输入值然后程序再次要求输入 A、B 或 X 而没有执行正确的操作)。输入后让它运行的任何帮助都会很棒!

         class output
         {
         static void Main(string[] args)
         {
            char userInput;
            char upper;

            Accounts myAccounts = new Accounts();
            myAccounts.input();
            Console.WriteLine("Enter an A to search account numbers");
            Console.WriteLine("Enter a B to average the accounts");
            Console.WriteLine("Enter X to quit the program");
            Console.WriteLine("Enter an option --->");

            userInput = Convert.ToChar(Console.ReadLine());
            upper = char.ToUpper(userInput);

            while (upper != 'X')
            {

                if (upper == 'A')
                {
                    myAccounts.search();
                    Console.WriteLine("Enter an A to search account numbers");
                    Console.WriteLine("Enter a B to average the accounts");
                    Console.WriteLine("Enter X to quit the program");
                    Console.WriteLine("Enter an option --->");
                    userInput = Convert.ToChar(Console.ReadLine());
                    upper = char.ToUpper(userInput);
                }
                else if (upper == 'B')
                {
                    myAccounts.average();
                    Console.WriteLine("Enter an A to search account numbers");
                    Console.WriteLine("Enter a B to average the accounts");
                    Console.WriteLine("Enter X to quit the program");
                    Console.WriteLine("Enter an option --->");
                    userInput = Convert.ToChar(Console.ReadLine());
                    upper = char.ToUpper(userInput);
                }
                else
                    Console.WriteLine("You entered an incorrect option, please select new option");
                Console.WriteLine("Enter an A to search account numbers");
                Console.WriteLine("Enter a B to average the accounts");
                Console.WriteLine("Enter X to quit the program");
                Console.WriteLine("Enter an option --->");
                userInput = Convert.ToChar(Console.ReadLine());
                upper = char.ToUpper(userInput);
            }        
        }
    }

【问题讨论】:

  • Srsly.. 了解方法... 例如PrintMenu()EvalInput(char input)等..
  • 平均、搜索和输入法都有什么?

标签: c# while-loop


【解决方案1】:

尝试像这样重新格式化您的主文件:

 static void Main(string[] args)
 {
    char userInput;
    char upper;

    Accounts myAccounts = new Accounts();
    myAccounts.input();

    do
    {

        Console.WriteLine("Enter an A to search account numbers");
        Console.WriteLine("Enter a B to average the accounts");
        Console.WriteLine("Enter X to quit the program");
        Console.WriteLine("Enter an option --->");
        userInput = Convert.ToChar(Console.ReadLine());
        upper = char.ToUpper(userInput);

        if (upper == 'A')
        {
            myAccounts.search();
        }
        else if (upper == 'B')
        {
            myAccounts.average();

        }
        else
            Console.WriteLine("You entered an incorrect option, please select new option");
    }   
    while (upper != 'X')     
}

它并不完美,但它是一个很好的起点。基本上,为什么您会看到两次提示,是因为您在进入 while 循环之前显示了一次菜单,然后在执行操作后再次显示。我提供的 do while 循环应该可以解决您的问题。

【讨论】:

  • 如果一个流程恰好执行,他正在打印菜单,当流程退出时。这个问题真的很浪费时间。如果他把牙套放在其他地方,他就会意识到问题所在。事实上, else 上没有大括号让我怀疑这是一个故意的问题(愤世嫉俗)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-16
  • 2020-02-15
  • 2019-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多