如何在C#中循环并检查正确的用户输入?

时间:2013-12-04 06:16:35

标签: c# loops

在下面的主要方法中,我正在尝试编码,所以如果用户没有输入正确的响应,那么代码将循环直到用户提供正确的响应。

我知道我在这里偏离中心,但我正在弄清楚如何纠正这个问题。

请记住,我想要保持这个简单和基本。

public static void Main()
    {
        string name = "";
        float startBal = 0;
        int acctNum = 0;
        string userInput = "";
        float[] deposits = new float[30];
        float[] withdrawls = new float[30];

        DisplayIntroduction();
        name = GetName();
        startBal = GetStartBal();
        acctNum = CreateAccount();

        Console.WriteLine("\n{0}, here is your new account # {1}\n", name, acctNum);

        do
        {
            Console.Write("Enter a 'D' to Deposit, 'W' to Withdrawl, or 'X' to End the program: ");
            userInput = Convert.ToString(Console.ReadLine());

            if (userInput.ToUpper() == "D")
            {
                //do the deposit, modify the deposit array
                Console.WriteLine("You entered D");
            }
            else if (userInput.ToUpper() == "W")
            {
                //do the withdrawl, modify the withdrawl array
                Console.WriteLine("You entered W");
            }
            else if (userInput.ToUpper() == "X")
            {
                //end the program, clear screen and display the summary
                Console.WriteLine("You entered X");
            }
            else
            {
                Console.WriteLine("Please enter a valid option:");

            }
        } while (userInput.ToUpper != "D" || userInput.ToUpper != "W" || userInput.ToUpper != "X");


    }

5 个答案:

答案 0 :(得分:5)

常见的错误,应该是你的一些AND运算符,而不是OR。

while (userInput.ToUpper != "D" && userInput.ToUpper != "W" && userInput.ToUpper != "X")

如果它与D不同而不是W,你想要循环。否则它将永远为真,因为它不能同时为D和W.

例如,如果用户输入D,则会获得false true true,但您需要全局结果为false才能退出循环。使用AND,您将获得false的全局结果,而使用OR,您将获得true。

你也可以使用一些LINQ来使它更紧凑(并且更不容易出错)(适用于很多元素):

while(!new[] {"D", "W", "X"}.Contains(userInput));

答案 1 :(得分:1)

检查两件事情是没有效率的,您正在user inputif-statements中再次检查while-condition

最简单的方法是设置bool类型变量,以指示用户是否输入了正确的答案。

bool flag = false;
do{ 
//your code
}
while(!flag)

并在每个if-statement中添加:

flag = true;

表示用户输入了正确的值。

答案 2 :(得分:0)

Pierre-Luc Pineault是正确的..但从逻辑上讲,这样做最好......

while(!(userInput.ToUpper == "D" || userInput.ToUpper == "W" || userInput.ToUpper == "X"))

答案 3 :(得分:0)

“while”条件意味着循环将一直触发,直到语句为假。

(userInput.ToUpper != "D" || userInput.ToUpper != "W" || userInput.ToUpper != "X");

将永远循环。

如果用户输入“D”例如

false OR true或true

你会想要一个&代替

(userInput.ToUpper != "D" && userInput.ToUpper != "W" && userInput.ToUpper != "X");

如果用户输入“D”

false AND true AND true = false

(爆发)

如果用户输入“Z”

真实,真实 = true(保持循环)

答案 4 :(得分:0)

我的版本是这样的:

// While loop usually read better: keep on asking again and again until 
// valid value is chosen
while (true) {
  Console.Write("Enter a 'D' to Deposit, 'W' to Withdrawl, or 'X' to End the program: ");
  String userInput = Console.ReadLine(); // <- You don't need any Convert here

  // Try not use ".ToUpper" - move it to comparison: StringComparison.OrdinalIgnoreCase
  if (String.Equals(userInput, "D", StringComparison.OrdinalIgnoreCase)) {
    Console.Write("You've entered 'D'");

    break;
  }
  else if (String.Equals(userInput, "W", StringComparison.OrdinalIgnoreCase)) {
    Console.Write("You've entered 'W'");

    break;
  }
  else if (String.Equals(userInput, "X", StringComparison.OrdinalIgnoreCase)) {
    Console.Write("You've entered 'X'");

    break;
  }

  Console.WriteLine();
  Console.WriteLine("You've written an invalid option.");
}