I have a homework where I have to make a program that asks numbers for ten times in a loop and in the end gives the sum of those ten numbers.
(我有一个家庭作业,我必须制作一个循环询问十次数字的程序,最后给出十个数字的总和。)
But it also must be possible to stop the loop and give the sum of numbers when enter is pressed. (但是也必须有可能停止循环并在按下Enter键时给出数字的总和。)
I have tried something like this in while loop, but it didn't also work:
(我在while循环中尝试过类似的方法,但是它也没有用:)
if( Console.KeyAvailable && Console.ReadKey( true ).Key == ConsoleKey.Enter ) break;
(if(Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Enter)中断;)
Can anyone suggest solutions?
(谁能建议解决方案?)
Thanks in advance!
(提前致谢!)
{
int i = 0;
int number;
string number_s;
int sum = 0;
while (i < 10)
{
{
i++;
Console.WriteLine("Enter number:");
number_s = Console.ReadLine();
Int32.TryParse(number_s, out number);
sum += number;
}
}
Console.Write("Sum is: {0}", sum);
}
ask by Lilian translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…