如何在c#中获得一行中的多个输入

  • 本文关键字:一行 c# input
  • 更新时间 :
  • 英文 :


c#代码,我不能解决这个问题:

int a, b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(a);
Console.WriteLine(b);

Console.ReadLine();

谁来帮帮我。

您可以使用以下示例:

(int a,int  b) = (Convert.ToInt32(Console.ReadLine()),Convert.ToInt32(Console.ReadLine()));

设法在一行中使用Linq来完成。但是说真的,只要用几行就行了。

(int? a, int? b) = 
Console
.ReadLine()
.Split(' ')
.Select(x => Convert.ToInt32(x))
.Aggregate(((int?)null, (int?)null), (l, next) => l.Item1 == null ? (next, (int?)null) : (l.Item1, next));

读取输入并拆分:

string input = Console.ReadLine();
var parts = input.Split(' ');
int a = Convert.ToInt32(parts[0]);
int b = Convert.ToInt32(parts[1]);