C# 将用户输入的整数存储在数组中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19146058/
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
Storing User Input Integers in an Array
提问by kar
I am trying to ask the user to input 10 numbers. After receiving the numbers, I am storing them in an array followed by printing the array. I came up with the following code to do the task but it is not printing the array.
我试图让用户输入 10 个数字。收到数字后,我将它们存储在一个数组中,然后打印该数组。我想出了以下代码来完成任务,但它没有打印数组。
Also feel that I may have rattled on way too much code for a simple task. Do note that I am very new to c# thus not familiar with advanced stuff or possibly even most of basic stuff. Even the "convert.toInt32", I adopted from reading around and not taught in class yet.
还觉得我可能已经为一个简单的任务编写了太多的代码。请注意,我对 c# 非常陌生,因此不熟悉高级内容,甚至可能不熟悉大多数基本内容。即使是“convert.toInt32”,我也是从阅读中采用的,还没有在课堂上教过。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test_Array
{
class Program
{
static void Main(string[] args)
{
int a;
int b;
int c;
int d;
int e;
int f;
int g;
int h;
int i;
int j;
Console.WriteLine("Please input 10 numbers. Press 'ENTER' after each number.");
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
c = Convert.ToInt32(Console.ReadLine());
d = Convert.ToInt32(Console.ReadLine());
e = Convert.ToInt32(Console.ReadLine());
f = Convert.ToInt32(Console.ReadLine());
g = Convert.ToInt32(Console.ReadLine());
h = Convert.ToInt32(Console.ReadLine());
i = Convert.ToInt32(Console.ReadLine());
j = Convert.ToInt32(Console.ReadLine());
int[] newArray = {a,b,c,d,e,f,g,h,i,j};
Console.WriteLine(newArray);
Console.ReadLine();
}
}
}
采纳答案by Servy
The ToString
method of arrays (which is what Console.WriteLine
is calling in your code) isn't overloaded to print out the contents of the array. It leaves the basic object
implementation of just printing the type name.
ToString
数组的方法(即Console.WriteLine
在您的代码中调用的方法)不会重载以打印出数组的内容。它留下了object
仅打印类型名称的基本实现。
You need to manually iterate the array and print out the individual values (or use a method that will do that for you).
您需要手动迭代数组并打印出各个值(或使用可以为您执行此操作的方法)。
I.e.
IE
foreach(var item in array)
Console.WriteLine(item)
or
或者
Console.WriteLine(string.Join("\n", array));
回答by user2711965
use a for
loop.
使用for
循环。
int[] newArray = new int[10];
for (int i = 0; i < newArray.Length; i++)
{
newArray[i] = Convert.ToInt32(Console.ReadLine());
}
You can use the same loop to display as well:
您也可以使用相同的循环来显示:
for (int i = 0; i < newArray.Length; i++)
{
Console.WriteLine(newArray[i]);
}
回答by Andrew Coonce
You could simplify things a lot with the following:
您可以通过以下方式简化很多事情:
static void Main(string[] args)
{
int newArray = new int[10];
Console.WriteLine("Please input 10 numbers. Press 'ENTER' after each number.");
for (int i = 0; i < 10; i++) {
newArray[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("The values you've entered are:");
Console.WriteLine(String.Join(", ", newArray));
Console.ReadLine();
}
回答by sudharsan
static void Main(string[] args)
{
int[] rollno = new int[10];
Console.WriteLine("Enter the 10 numbers");
for (int s = 0; s < 9; s++)
{
rollno[s] = Convert.ToInt32(Console.ReadLine());
rollno[s] += 110;
}
for (int j = 0; j < 9; j++)
{
Console.WriteLine("The sum of first 10 numbers is : {0}", rollno[j]);
}
Console.ReadLine();
}
}
}
}