My C#代码打印随机数而不是总和。

huangapple go评论63阅读模式
英文:

My C# code printing random numbers instead of the sum

问题

I am new to C# and was trying to solve the following problem:

Write a program in C# to find the sum of all elements of the array. Go to the editor
Test Data:

Input the number of elements to be stored in the array: 3
Input 3 elements in the array:
element - 0: 2
element - 1: 5
element - 2: 8

Expected Output:

Sum of all elements stored in the array is: 15

Here is the solution I came up with:

Console.Write("Enter the length of the array : ");
int len = Convert.ToInt32 (Console.ReadLine());
int[] arr = new int[len];
for (int i = 0; i < len; i++) {
    arr[i] = Convert.ToInt32(Console.Read());
}
Console.WriteLine(arr.Sum());

But I am getting the following outputs:
My C#代码打印随机数而不是总和。

Can somebody please tell me what I am doing wrong here?

英文:

I am new to C# and was trying to solve the following problem:

> Write a program in C# to find the sum of all elements of the array. Go to the editor
> Test Data:
> none
&gt; Input the number of elements to be stored in the array: 3
&gt; Input 3 elements in the array:
&gt; element - 0: 2
&gt; element - 1: 5
&gt; element - 2: 8
&gt;

> Expected Output:
> none
&gt; Sum of all elements stored in the array is: 15
&gt;

Here is the solution I came up with:

Console.Write(&quot;Enter the length of the array : &quot;);
int len = Convert.ToInt32 (Console.ReadLine());
int[] arr = new int[len];
for (int i = 0; i &lt; len; i++) {
    arr[i] = Convert.ToInt32(Console.Read());
}
Console.WriteLine(arr.Sum());

But I am getting the following outputs:
My C#代码打印随机数而不是总和。

Can somebody please tell me what I am doing wrong here?

答案1

得分: 3

Try changing Console.Read to Console.ReadLine and using int.Parse directly:

for (int ii = 0; ii &lt; len; ii++) {
    arr[ii] = int.Parse(Console.ReadLine());
}

And then enter numbers on different lines (note that usually int.TryParse is recommended to use to validate the input because int.Parse will throw an exception if the string can't be parsed into a number).

Console.Read already returns an int which is:

The next character from the input stream, or negative one (-1) if there are currently no more characters to be read.

This represents the encoded character (including spaces, letters, etc.) value. For example, if you enter 1, you will get 49 as the result (try for example Console.WriteLine((int)'1')).

英文:

Try changing Console.Read to Console.ReadLine and using int.Parse directly:

for (int ii = 0; ii &lt; len; ii++) {
    arr[ii] = int.Parse(Console.ReadLine());
}

And then enter numbers on different lines (note that usually int.TryParse is recommended to use to validate the input, because int.Parse will throw if string can't be parsed into a number).

Console.Read already returns an int which is:

> The next character from the input stream, or negative one (-1) if there are currently no more characters to be read.

Which represents encoded character (including spaces, letters, etc.) value. I.e. if you enter 1 you will get 49 as the result (try for example Console.WriteLine((int)&#39;1&#39;)).

答案2

得分: 1

To explain the 131 result, remember the input is a string 1 2 3. This includes the spaces. The Console.Read() function returns an int value for each character, including spaces.

So what you're seeing is the sum of the ascii<sup>*</sup> int values of the first three characters in that string: 1 (49), 2 (50), and the space character between them (32), which gives you 131 as the result.


<sub>* C# actually uses unicode, but the Windows (or other) console may be using something different. For this example, however, they'll all be ascii-compatible.</sub>

英文:

To explain the 131 result, remember the input is a string 1 2 3. This includes the spaces. The Console.Read() function returns an int value for each character, including spaces.

So what you're seeing is the sum of the ascii<sup>*</sup> int values of the first three characters in that string: 1 (49), 2 (50), and the space character between them (32), which gives you 131 as the result.


<sub>* C# actually uses unicode, but the Windows (or other) console may be using something different. For this example, however, they'll all be ascii-compatible.</sub>

huangapple
  • 本文由 发表于 2023年4月10日 23:11:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定