做while循环不起作用。 do语句在重复

时间:2019-07-02 17:36:09

标签: c#

我的代码中存在逻辑错误。我不知道自己在哪里搞砸了。我是C#的新手。

do{ }正在重复,即使我输入了正确的值或错误的值也是如此。它不会转到while语句

do
{
    Console.WriteLine("What is you gender?");
    gender = Console.ReadLine();
}
while (gender != "male" || gender != "female" || gender != "others");
{
    Console.WriteLine("Please enter your gender.");
}

2 个答案:

答案 0 :(得分:3)

尝试类似的方法(未经测试):

Console.WriteLine("Please enter your gender.");    
do
{
            Console.WriteLine("What is you gender?");
            gender = Console.ReadLine().ToLower();
}
while (gender != "male" && gender != "female" && gender != "others");

这可确保循环至少执行一次,但会提示每次尝试。初始的writeline是可选的

答案 1 :(得分:2)

使用“和”符号&&,例如:

do
{
   Console.WriteLine("What is you gender?");
   gender = Console.ReadLine();
}
while (gender != "male" && gender != "female" && gender != "others");