Read numbers from the console given in a single line, separated by a space

前端 未结 7 1765
小蘑菇
小蘑菇 2020-12-01 12:58

I have a task to read n given numbers in a single line, separated by a space ( ) from the console.

I know how to do it whe

7条回答
  •  有刺的猬
    2020-12-01 13:46

    You can use Linq to read the line then split and finally convert each item to integers:

      int[] numbers = Console
            .ReadLine()
            .Split(new Char[] {' '}, StringSplitOptions.RemoveEmptyEntries)
            .Select(item => int.Parse(item))
            .ToArray();
    

提交回复
热议问题