gpt4 book ai didi

c# - 写出输入数字到控制台应用程序,C#

转载 作者:行者123 更新时间:2023-11-30 20:55:30 24 4
gpt4 key购买 nike

我有一个程序要求输入一个数字 (int x)。然后,用户应该向控制台输入 x 个数字。并且控制台应该将所有数字加在一起并写出所有输入数字的结果。所以我这样做了:

Console.WriteLine("Enter an number: ");
int x = int.Parse(Console.ReadLine());

for (int i = 0; i < x; i++ )
{
Console.WriteLine("Ange tal {0}: ",i );
double numbers= double.Parse(Console.ReadLine());
}

Console.WriteLine("Sum of the entered numbers are: {0} ",x);
Console.ReadLine();

但结果只给我最后输入的数字。我做错了什么?

最佳答案

您需要创建一个变量来存储数字的总和 (sum)。然后在你读到下一个数字后,你应该把它加到你的总和上。

Console.WriteLine("Enter a number: ");
int x = int.Parse(Console.ReadLine());
double sum = 0;
for (int i = 0; i < x; i++ )
{
Console.WriteLine("Ange tal {0}: ", i);
double number = double.Parse(Console.ReadLine());
sum += number;
}

Console.WriteLine("Sum of the entered numbers is: {0}", sum);
Console.ReadLine();

关于c# - 写出输入数字到控制台应用程序,C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18253045/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com