gx1*_*x15 2 c# arrays int for-loop
我需要代码,从用户那里获取输入,然后将它们加在一起 - 简单.但是我找不到方法,接受输入直到0被按下并且将数字加在一起..到目前为止,我做了它需要10个值,但就像我说它需要自定义..感谢您的帮助.
int[] myarray = new int[10];
for (int i = 0; i < 10; i++)
{
myarray[i] = Convert.ToInt32(Console.ReadLine());
}
int a = 0;
for (int j = 0; j < 10; j++)
{
a = a + myarray[j];
}
Console.WriteLine(a);
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)
下面的代码不限于10个输入,您可以根据需要提供尽可能多的输入
int sum=0, input;
do
{
input = Convert.ToInt32(Console.ReadLine());
sum += input;
}
while(input != 0);
Console.WriteLine(sum);
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)