使用do-while循环重启C#程序的简单是/否

时间:2017-02-15 00:10:47

标签: c# while-loop do-while restart

我无法弄清楚为什么我的字符串重新启动会更改为"否"无论我在这一点上输入什么:

while (
  (restart != "YES") && (restart != "NO")
)

非常感谢任何帮助。

do
{
    //main part of program
    Console.Write("Do you wish to calculate another? (YES/NO) ");
    restart = Console.ReadLine();
    restart = qualified.ToUpper();
    while ((restart != "YES") && (restart != "NO"))   //????
    {
        Console.WriteLine("Error");
        Console.WriteLine("Do you wish to calculate another? (YES/NO) ");
        restart = Console.ReadLine();
        restart = qualified.ToUpper();
    }
} while (restart == "YES");

2 个答案:

答案 0 :(得分:2)

永远不会分配

qualified.ToUpper(),因此保持不变。无论用户输入什么,您的restart变量始终会被赋予相同的值。拿出来然后使用:

do
{
    //main part of program
    Console.Write("Do you wish to calculate another? (YES/NO) ");
    restart = Console.ReadLine().ToUpper();
    while ((restart != "YES") && (restart != "NO"))   //????
    {
        Console.WriteLine("Error");
        Console.WriteLine("Do you wish to calculate another? (YES/NO) ");
        restart = Console.ReadLine().ToUpper();
    }
} while (restart == "YES");

答案 1 :(得分:1)

删除以下代码:

restart = qualified.ToUpper();