将输入数字写入控制台应用程序,C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18253045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:41:16  来源:igfitidea点击:

Writing out input numbers to console application,C#

c#numbersconsole-application

提问by user2669196

I have a program that asks for a number (int x). then, the user should input x numbers to the console. And the console should add all the numbers together and write out the result of all the input numbers. So I've done this:

我有一个程序要求输入一个数字(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();

But the the result only gives me the last entered number. What am I doing wrong?

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

回答by Sergey Gavruk

You need to make a variable where you will store the sum of the numbers (sum). Then after you read the next number, you should add it to your sum.

您需要创建一个变量来存储数字的总和 ( 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();

回答by CaveCoder

This way the sum of the inputed number will be shown

这样将显示输入数字的总和

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

        }

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

回答by Deeko

You never actually do any summation in your code.

您实际上从未在代码中进行任何求和。

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

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

回答by Ehsan

You can do it like this

你可以这样做

Console.WriteLine("Enter an number: ");
int x = int.Parse(Console.ReadLine());
List<double> allNumbers = new List<double>();
for (int i = 0; i < x; i++ )
{
      Console.WriteLine("Ange tal {0}: ",i );
      double temp;
      if(double.TryParse(Console.ReadLine(), out temp))
         allNumbers.Add(temp);
      else
          Console.WriteLine("Enter a valid number");  
}

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

回答by xanatos

Here there is the code, with the Writeand WriteLinecorrectly formatted.

在这里有代码,与WriteWriteLine格式正确无误。

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

double sum = 0;

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

Console.WriteLine("Sum of the entered numbers are: {0:R} ", sum);
Console.Write("Press a key to exit");
Console.ReadKey();

But now we want to go a step forward: try inserting:

但是现在我们想更进一步:尝试插入:

2
0.1
0.2

(or 0,1and 0,2if you use ,as the decimal separator)

(或者0,10,2如果您,用作小数点分隔符)

I always think that OMG Ponies!!! (Aka Humanity: Epic Fail)is the best reading possible...

我一直认为OMG小马!!!(又名人类:史诗失败)是最好的阅读可能......

回答by Umer Mushtaq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace add

{
    class Program
    {
        static void Main(string[] args)
        {
            int a,b,c;
            Console.WriteLine("Enter the first number");
            a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter second number");
            b = Convert.ToInt32(Console.ReadLine());
            c = a + b;
            Console.WriteLine("The addition of two number is {0}", c);
            Console.ReadLine();
        }
    }
}

回答by Dido Viktorov

    Console.Write("Enter N number: ");
    double numberN = double.Parse(Console.ReadLine());
    double sum = 0;

    for (double i = 0; i < numberN; i++)
    {

        Console.Write("Enter number: ");
        double number = double.Parse(Console.ReadLine());
        sum += number;

    }

    Console.WriteLine("The sum is: {0}", sum);