How to enter three numbers from the keyboard into the console through a space in the C # programming language?

    2 answers 2

    static void Main(string[] args) { string line = Console.ReadLine(); string[] splitLine = line.Split(' '); foreach (var sLine in splitLine) { Console.WriteLine(sLine); } Console.ReadKey(); } 
    • Explain in more detail, please, the decision step by step. Didn't quite understand foreach - Redl
    • one
      @ Redl, you enter the numbers through the space in the console and press Enter. Got a string and execute Split by a space, you end up with an array of numbers, then foreach displays them on the screen. - iluxa1810

    It is necessary to use string str=Console.ReadLine(); After this instruction, the user will be expected to enter the console and by pressing Enter the data will be assigned to the variable str.

    It must be remembered that ReadLine () returns the data in a string representation and therefore, if you need numbers for some arithmetic operations, then you must do an explicit conversion. For example, if you want to convert to an integer, you can call Int32.TryParse . For other numeric types there are similar conversion functions.

    In order to read several numbers, you must either enter numbers through a separator, and then execute Split or in a loop call string str=Console.ReadLine(); N-th number of times.

    • How can you use Split if a string is entered, and you need to assign separate numbers to separate variables separated by a space in the entered string? - Redl
    • @Redl, Is there a divider in the line? - iluxa1810
    • , yes, three numbers are entered, separated by spaces - Redl
    • @Redl, below tCode gave an example. It is correct, just note that if you need arithmetic operations, then you need to convert the string to a number explicitly. - iluxa1810
    • and how now each number that we received in the array, translate from string to number? - Redl