我似乎无法使用break语句突破这个while
循环。这是我的代码:
while (true) {
Console.WriteLine("What is 1 + 1?");
int answer = Convert.ToInt32(Console.ReadLine());
if (answer != 2)
{
Console.WriteLine("Wrong answer. Press enter to get your next sum");
Console.ReadLine();
}
else if (answer == 2)
{
Console.WriteLine("Well done. Press enter to get your next sum");
Console.ReadLine();
break;
}
}
Console.WriteLine("What is 1 + 3");
int answer2 = Convert.ToInt32(Console.ReadLine());
答案 0 :(得分:6)
当您输入不同于2的数字时,您实际上读取了另一行未处理的行:
if (answer != 2)
{
Console.WriteLine("Wrong answer. Press enter to get your next sum");
Console.ReadLine();
}
你的困惑可能是因为这条线。将其更改为:
if (answer != 2)
{
Console.WriteLine("Wrong answer. Press enter to get your next sum");
}
else{
Console.WriteLine("Correct answer");
break;
}