繁体   English   中英

如何编写一个控制台应用程序来计算给定整数的总和

[英]how to write a console application that calculates the sum of a given number of integers

编写一个控制台应用程序,以计算给定数量的整数之和。 每行输入一个数字,应用程序将一一读取,直到用户输入字符而不是数字为止。 当用户键入x时,应用程序将知道已输入字符串中的所有数字并显示其数量。

如果用户输入的第一件事是x字符,则应用程序将返回0。

例:

输入:

2
5
-3
1
X

控制台将显示:

5

这是我的代码

string[] answer = new string[10];
int sum = 0

for (int i = 0; i < answer.Length; i++)
{
    sum += Int32.Parse(answer[i]);

    if (answer[i] == "x")
    {
        Console.WriteLine(sum);
    }

    answer[i] = Console.ReadLine();
}

Console.Read();

谁能告诉我为什么不起作用?

首先,工作代码(我不是专注于X,而是专注于不是数字的任何char):

int n;
int sum = 0;
while (int.TryParse(Console.ReadLine(), out n))
{
    sum += n;
}
Console.Write(sum ); 
Console.ReadKey();

其次,您的代码无法正常工作,因为当您尝试解析“ answer [i]”中其第一个单元格的内容时,您的数组充满了“ null”

这是您的代码的愚蠢(一点点)修复:

string[] answer = new string[10];

//HERE
for (int i = 0; i < answer.Length; i++)
{
    answer[i] = "0";
}    

int sum = 0;
for (int i = 0; i < answer.Length; i++)
{
    sum += Int32.Parse(answer[i]);
    if (answer[i] == "x")
    {
        Console.WriteLine(sum);
    }
    answer[i] = Console.ReadLine();
}
Console.Read();

您的代码的另一个问题是,一旦输入“ x”,您就不会停止迭代,而是继续到数组的末尾(直到它结束了10次)。

这是对您的代码的完整修复:

string[] answer = new string[10];
for (int i = 0; i < answer.Length; i++)
{
    answer[i] = "0";
}
int sum = 0;

for (int i = 0; i < answer.Length; i++)
{
    answer[i] = Console.ReadLine();
    if (answer[i] == "x")
    {
        break;
    }
    sum += Int32.Parse(answer[i]);
}
Console.WriteLine(sum);
Console.Read();

几个问题:

  1. 我认为您的代码指令顺序不正确。 第一次解析数组元素时,它尚未初始化。
  2. 缺少int sum = 0 ; 在末尾。
  3. 您应该始终使用TryParse而不是Parse

尝试以下代码:

string[] answer = new string[10];
int sum = 0, number;
for (int i = 0; i < answer.Length; i++)
{
   answer[i] = Console.ReadLine();
   if (answer[i] == "x")
   {
      Console.WriteLine(sum);
      break;
   }
   if(Int32.TryParse(answer[i], out number))
      sum += number;
}

我给了你结尾的“ x”

var answer = Console.ReadLine();
var sum = 0;

while (answer != "x")
{    
    if (Int32.TryParse(answer, out var value))
    {
        sum += value;
    }
    answer = Console.ReadLine();    
}
Console.WriteLine(sum);

您应该检查"x" 第一 ,因为int.Parse("x") 抛出异常

订单错误(当前代码):

 sum += Int32.Parse(answer[i]); // <- this will throw exception on "x" input

 if (answer[i] == "x") {
   ...
 }
 ... 

正确的顺序:

 if (answer[i] == "x") {
   ...
 }
 else 
   sum += Int32.Parse(answer[i]);
 ...  

为了检查语法错误(例如,当用户输入"bla-bla-bla" ),我建议使用int.TryParse而不是int.Parse ,让我们摆脱数组的原因,为什么我们应该收集项目(尤其是对10项目)?

 // long: we don't want overflow, e.g. 2000000000, 1000000000 
 long sum = 0;

 while (true) {
   // Trim() - let's be nice and allow user put leading/trailing spaces
   string input = Console.ReadLine().Trim();

   if (string.Equals("x", input, StringComparison.OrdinalIgnoreCase))
     break;

   if (int.TryParse(input, out var item))
     sum += item;
   else {
     //TODO: Incorrect input, neither integer nor "x" (e.g. "abracadabra")  
   }   
 }

 Console.WriteLine(sum);

 Console.Read(); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM