我正在编写一个程序,从用户那里获取10个数字(控制台应用程序),创建这些数字的总和和平均值,并输出总和和平均值。
我能够通过for循环执行此操作,但不知道如何将相同的逻辑传递给do while循环。
int sum, i, n;
sum = 0;
double avg;
Console.Write("Input the 10 numbers : ");
for (i=1;i<=10;i++)
{
Console.Write("Number-{0} :",i);
n= Convert.ToInt32(Console.ReadLine());
sum +=n;
}
avg=sum/10.0;
Console.Write("The sum of 10 numbers is : {0}\nThe Average is : {1}\n",sum,avg);
答案 0 :(得分:0)
int sum = 0, i = 0, n = 0;
double avg = 0.0d;
Console.Write("Input the 10 numbers : ");
do
{
Console.Write("Number-{0} :", i);
n = Convert.ToInt32(Console.ReadLine());
sum += n;
++i;
} while(i < 10);
avg = sum / 10.0;
Console.WriteLine($"The sum of 10 numbers is : {sum}\nThe Average is : {avg}");
使用online C# runner (rextester)进行测试。