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

前端 未结 7 1805
小蘑菇
小蘑菇 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:45

    you can do

    int[] Numbers  = Array.ConvertAll(Console.ReadLine().Split(' '),(item) => Convert.ToInt32(item));
    

    the above line helps us get individual integers in a Line , separated by a Single space.Two Or More spaces between numbers will result in error.

    int[] Numbers = Array.ConvertAll(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), (item) => Convert.ToInt32(item));
    

    this variation will Fix the error and work well even when two or more spaces between numbers in a Line

提交回复
热议问题