开发者

Reading two inputs from the user

开发者 https://www.devze.com 2023-03-10 08:45 出处:网络
I would like to get the user enters two different开发者_Python百科 values using only ONE statement of Console.ReadLine().

I would like to get the user enters two different开发者_Python百科 values using only ONE statement of Console.ReadLine(). More specifically, instead of using two different variables declarations with two different ReadLine() method - I want to let the user enters two variables at the same time using one ReadLine() method for further processing.


Something like this?

var line = Console.ReadLine();
var arguments = line.Split(' ');
var arg1 = arguments[0];
var arg2 = arguments[1];

In this example, space delimits the arguments. You can use , or ; as a delimiter, if you would like to.


Console.WriteLine("Enter values separated by space");
string input = Console.ReadLine();
string[] inputs = input.Split(' ');
foreach (string inp in inputs)
{
     Console.WriteLine(inp);  
}
0

精彩评论

暂无评论...
验证码 换一张
取 消