英文:
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:
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
> 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:
> none
> 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:
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 < 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 < 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)'1')
).
答案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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论