小编Jey*_*ite的帖子

需要两次尝试才能打破我的while循环

我正在做Udemy课程,并试图完成一个练习,要求用户输入数字,或者如果我写“ quit”,则中断循环。循环后,我必须对所有数字求和。

我几乎可以正常工作了,但是我不得不写两次 “退出” 才能使其崩溃,但我不明白为什么。感谢任何帮助

int[] total = new int[10];
int number;
int counter = 0;
Console.WriteLine("Write up to 10 number or 'quit' to exit early");
while (counter < 10)
{
    bool success = int.TryParse(Console.ReadLine(), out number);
    if (success == true)
    {
        total[counter] = number;
        counter++;
    } else if (Console.ReadLine() == "quit")
    {
        break;
    } else
    {
        Console.WriteLine("Wrong input.");
    }
}
int sum = total.Sum();
Console.WriteLine("The sum is {0}", sum);
Run Code Online (Sandbox Code Playgroud)

c# tryparse while-loop

1
推荐指数
1
解决办法
84
查看次数

标签 统计

c# ×1

tryparse ×1

while-loop ×1