读取由空间隔开的N长度阵列
您知道从控制台中获取用户输入的更好方法吗?输入的第一行由单个数字N组成,即我们的数组的长度。二线的n个数字被空间隔开。 我的代码看起来像这样:
int n = int.Parse(Console.ReadLine());
int[] arr = new int[n];
string[] stringarray;
stringarray = Console.ReadLine()).Split(' ');
for (int i = 0; i < n; i++)
{
arr[i] = Convert.ToInt32(stringarray[i]);
}
Do you know a better way to take user input from the console? The first line of input consists of a single number n, which is the length of our array. Second-line has n numbers separated by spaces.
My code looks like this:
int n = int.Parse(Console.ReadLine());
int[] arr = new int[n];
string[] stringarray;
stringarray = Console.ReadLine()).Split(' ');
for (int i = 0; i < n; i++)
{
arr[i] = Convert.ToInt32(stringarray[i]);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用LINQ而不是像以下线这样的循环:
最终代码:
You can use Linq instead of loop like this line:
Final Code: