如何在C#中的同一行中接受多个输入?

如何在C#中的同一行中接受多个输入?,c#,.net,input,C#,.net,Input,我不熟悉C#编程。我想知道如何在一行中接受多个输入。我的代码: using System; namespace ArrayExercise01 { class Program { static void Main(string[] args) { int arraySize = int.Parse(Console.ReadLine()); int[] ar = new int[arraySize];

我不熟悉C#编程。我想知道如何在一行中接受多个输入。我的代码:

using System;

namespace ArrayExercise01
{
    class Program
    {
        static void Main(string[] args)
        {
            int arraySize = int.Parse(Console.ReadLine());
            int[] ar = new int[arraySize];
            int i;
            for(i=0; i<arraySize; i++) 
            {
                Console.Write("");
                ar[i] = int.Parse(Console.ReadLine());
            }
            int sum = 0;
            foreach (int arrayElement in ar)
            {
                sum += arrayElement;
            }
            Console.WriteLine(sum);
        }
    }
}
但我想做这样的事情(一行多输入):

而不是:

ar[i] = int.Parse(Console.ReadLine());
你可以:

foreach(string s in Console.ReadLine().Split()){
  ar[i] = int.Parse(s);
  i++;
}
不带任何参数的拆分在空格上拆分,
就是其中之一。。因此,系统会提示用户输入一个字符串,用户可以输入
1
1 2 3 4 5
或任意组合(但如果用户输入的数字超过剩余数组所能容纳的数字,则会出现问题)。代码将它们在空间上拆分为五个字符串,分别为
“1”
“2”
“3”
“4”
“5”
,然后循环运行5次,每个字符串依次被解析并存储在数组索引中。索引
i
每次递增


除了检查
i
是否仍在数组长度内,建议也跳过错误输入;看看int.TryParse,如果解析没有成功,它将返回一个布尔值false;如果返回false,请不要添加到数组中/不要增加
i

尝试此操作,但要小心,输入太多的数字将导致IndexAutoFrangeException

for (int i = 0; i < arraySize; )
{
    // first catch the input
    string input = Console.ReadLine();

    // split it by space
    string[] everyNumber = input.Split(' ');

    for (int j = 0; j < everyNumber.Length; j++)
    {
        // parse each number in string array
        ar[i] = int.Parse(everyNumber[j]);
        i++;
    }
}
for(int i=0;i
根据您对“在同一行上获取多个输入”的要求,您可以按如下操作

List<int> numbers = Console.ReadLine().Split().Select(int.Parse).ToList();
程序如下所示

static void Main(string[] args)
    {
        int arraySize = int.Parse(Console.ReadLine());

        List<int> numbers = Console.ReadLine().Split().Select(int.Parse).ToList();

        while (numbers.Any() && ((numbers.Count() < arraySize) || (numbers.Count() > arraySize)))
        {
            Console.WriteLine("You have enter the numbers might be less than or greater than given size");
            Console.WriteLine("Please the numbers of given size");
            numbers = Console.ReadLine().Split().Select(int.Parse).ToList();
        }

        int sum = 0;
        foreach (var num in numbers)
        {
            sum += num;
        }
        //or you can use directly sum method as follws
        //Console.WriteLine(numbers.Sum());

        Console.WriteLine(sum);
        Console.Read();
    }

你可以按空间分割,怎么分割?你能给我举个例子吗?查string.Split()。
List<int> numbers = Console.ReadLine().Split().Select(int.Parse).ToList();
while (numbers.Any() && ((numbers.Count() < arraySize) || (numbers.Count() > arraySize)))
{
  Console.WriteLine("You have enter the numbers might be less than or greater than given size");
  Console.WriteLine("Please the numbers of given size");
  numbers = Console.ReadLine().Split().Select(int.Parse).ToList();
}
int sum = 0;
foreach (var num in numbers)
{
  sum += num;
}
//or you can use directly sum method as follows
//Console.WriteLine(numbers.Sum());
static void Main(string[] args)
    {
        int arraySize = int.Parse(Console.ReadLine());

        List<int> numbers = Console.ReadLine().Split().Select(int.Parse).ToList();

        while (numbers.Any() && ((numbers.Count() < arraySize) || (numbers.Count() > arraySize)))
        {
            Console.WriteLine("You have enter the numbers might be less than or greater than given size");
            Console.WriteLine("Please the numbers of given size");
            numbers = Console.ReadLine().Split().Select(int.Parse).ToList();
        }

        int sum = 0;
        foreach (var num in numbers)
        {
            sum += num;
        }
        //or you can use directly sum method as follws
        //Console.WriteLine(numbers.Sum());

        Console.WriteLine(sum);
        Console.Read();
    }
3
1 2
You have enter the numbers might be less than or greater than given size
Please the numbers of given size
1 2 3 4
You have enter the numbers might be less than or greater than given size
Please the numbers of given size
1 2 3
6