i know how to make a console read two integers but each integer by it self like this
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
if i entered two numbers, i.e (1 2), the value (1 2), cant be parse to integers what i want is if i entered 1 2 then it will take it as two integers
in 1 line, thanks to LinQ and regular expression (no type-checking neeeded)
I have a much simpler solution, use a switch statement and write a message for the user in each case, using the Console.write() starting with a ("\n").
Here's an example of filling out an array with a for loop while taking user input. * Note: that you don't need to write a for loop for this to work* Try this example with an integer array called arrayOfNumbers[] and a temp integer variable. Run this code in a separate console application and Watch how you can take user input on the same line!
The magic trick here is that you're fooling the console application, the secret is that you're taking user input on the same line you're writing your prompt message on. (message=>"Enter First Number: ")
This makes user input look like is being inserted on the same line. I admit it's a bit primitive but it does what you need without having to waste your time with complicated code for a such a simple task.
Try this:
Hope it helps:)
One option would be to accept a single line of input as a string and then process it. For example:
One issue with this approach is that it will fail (by throwing an
IndexOutOfRangeException
/FormatException
) if the user does not enter the text in the expected format. If this is possible, you will have to validate the input.For example, with regular expressions:
Alternatively:
int.TryParse
to attempt to parse the strings into numbers.Read the line into a string, split the string, and then parse the elements. A simple version (which needs to have error checking added to it) would be: