从技术上讲,你可以实现任何循环,但我建议while
... string answer1 = Console.ReadLine(); while (answer1 != "yes" && answer1 != "no") { Console.WriteLine("Pardon me?"); answer1 = Console.ReadLine(); } ...
只是不断询问,而 answer1
不是正确的.
编辑:正如Hogan在评论中建议的那样,我们应该对用户很好:让他/她在任何带有前导和尾随空格的寄存器中输入YES/no:
... // with Trim() and ToUpper() all "Yes", " yes", "YES " are OK string answer1 = Console.ReadLine().Trim().ToUpper(); while (answer1 != "YES" && answer1 != "NO") { Console.WriteLine("Pardon me?"); answer1 = Console.ReadLine().Trim().ToUpper(); } ...
编辑2:退出程序(请参阅注释中的其他问题),只需从以下位置返回Main
:
... if (answer1 == "NO") { Console.WriteLine("Your loss"); return; // return from Main will exit the program }