指定在控制台中输入的行数 [英] Specify number of lines to input in console

查看:119
本文介绍了指定在控制台中输入的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我想要的输入示例:



输入:

2

1 2 3 4

3 2 5 1



输出:xxx是最大的总和。



两行输入

 1 2 3 4 

 3 2 5 1 

代表每个人,我想输出哪个人的金额最大。在这个例子中,第二个人拥有最大的金额。输入第一行中的数字2表示我想输入多少行输入。所以,如果我输入3,我想输入3个不同的人的总和。



我尝试了什么:



我一直无法理解。我只能输入无限数量的行,但这不是我想要的。这就是我得到的:



 string line; 
while((line = Console.ReadLine())!= null)
{
string [] split = line.Split(new char [] {''},StringSplitOptions.None) ;
long a1 = Int64.Parse(split [0]),b1 = Int64.Parse(split [1]),a2 = Int64.Parse(split [2]),b2 = Int64.Parse(split [3] ]);

Console.ReadLine();
}

解决方案

你可以像这样使用while循环:

 while(true)
{
string line = Console.ReadLine();

if(string.IsNullOrEmpty(line))
break;

Console.WriteLine(line);
}



或:

字符串行; 

do
{
line = Console.ReadLine();
Console.WriteLine(line);
}
while(!string.IsNullOrEmpty(line));


首先应该询问行数,确认输入确实是一个有效的整数,并将其存储在一个计数器中。 />
然后,一个循环将执行所需的行数。

  int 计数; 
while (!int.TryParse(Console.ReadLine(), out count)); // 执行直到提供有效整数
int i = 0 ;
while (i < count){
// 你的线路输入和解析
}



希望这可以帮助。麻烦。


Here's an example of the input i want:

Input:
2
1 2 3 4
3 2 5 1

Output: xxx has the biggest sum.

The two lines of input

1 2 3 4

and

3 2 5 1

represent a person each, and i want to output which person has the biggest sum. In this example the second person has the biggest sum. The number "2" in the first line of input represents how many lines of imput i want to type. So if i type 3 i want to input the sum for 3 different people.

What I have tried:

I haven't been able to figure it out. I can only input an infinite number of lines, but that's not really what i want. Here's what i got:

string line;
            while ((line = Console.ReadLine()) != null)
            {
                string[] split = line.Split(new char[] { ' ' }, StringSplitOptions.None);
                long a1 = Int64.Parse(split[0]), b1 = Int64.Parse(split[1]), a2 = Int64.Parse(split[2]), b2 = Int64.Parse(split[3]);

                Console.ReadLine();
            }

解决方案

You can use a while loop like this:

while (true)
{
    string line = Console.ReadLine();

    if (string.IsNullOrEmpty(line))
        break;

    Console.WriteLine(line);
}


Or:

string line;

do
{
    line = Console.ReadLine();
    Console.WriteLine(line);
}
while (!string.IsNullOrEmpty(line));


You should first ask for the number of lines, verify that input is indeed a valid integer, and store it in a counter.
Then, a loop which would execute the desired number of lines.

int count;
while (!int.TryParse(Console.ReadLine(), out count)); // Execute until a valid integer is provided
int i = 0;
while (i < count) {
   // your line input and parsing here
}


Hope this helps. Kindly.


这篇关于指定在控制台中输入的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆