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

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

    You can use String.Split. You can provide the character(s) that you want to use to split the string into multiple. If you provide none all white-spaces are assumed as split-characters(so new-line, tab etc):

    string[] tokens = line.Split(); // all spaces, tab- and newline characters are used
    

    or, if you want to use only spaces as delimiter:

    string[] tokens = line.Split(' ');
    

    If you want to parse them to int you can use Array.ConvertAll():

    int[] numbers = Array.ConvertAll(tokens, int.Parse); // fails if the format is invalid
    

    If you want to check if the format is valid use int.TryParse.

    0 讨论(0)
  • 2020-12-01 13:31

    you can use this function, it's very helpful

        static List<string> inputs = new List<string>();
        static int input_pointer = 0;
    
        public static string cin(char sep = ' ')
        {
            if (input_pointer >= inputs.Count)
            {
                string line = Console.ReadLine();
    
                inputs = line.Split(sep).OfType<string>().ToList();
                input_pointer = 0;
            }
    
            string v = inputs[input_pointer];
            input_pointer++;
    
            return v;
        }
    

    Example:

            for(var i =0; i<n ; i++)
                for (var j = 0; j<n; j++)
                {
                    M[i,j] = Convert.ToInt16(cin());
                }
    
    0 讨论(0)
  • 2020-12-01 13:43

    You can split the line using String.Split():

    var line = Console.ReadLine();
    var numbers = line.Split(' ');
    foreach(var number in numbers)
    {
        int num;
        if (Int32.TryParse(number, out num))
        {
            // num is your number as integer
        }
    }
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 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();
    
    0 讨论(0)
  • 2020-12-01 13:50

    You simply need to split the data entered.

    string numbersLine = console.ReadLine();
    
    string[] numbers = numbersLine.Split(new char[] { ' '});
    
    // Convert to int or whatever and use
    
    0 讨论(0)
提交回复
热议问题