简体   繁体   English

Java 2行终端输入

[英]Java 2 lines terminal input

For a school assignment I have to make a program that reads two numbers from the terminal and then processes those numbers. 对于学校作业,我必须编写一个程序,从终端读取两个数字,然后处理这些数字。 The program has to automatically process those two values once they have been entered. 输入这两个值后,程序必须自动处理这两个值。 The code I have so far is down below, but you have to press Enter before the program multiplies the numbers, the user shouldn't have to press enter three times, but only two times. 我到目前为止的代码在下面,但是您必须在程序将数字乘以之前按Enter键,用户不必按三次Enter键,而只需按两次即可。

public static void man(String[] args) throws NumberFormatException, IOException{
    BufferedReader reader = new BufferedReader( new InputStreamReader(System.in)); 
    int count = 0;
    int width = 0;
    int height= 0;
    String number;
    while( (number = reader.readLine())!=null  && count < 2 ) {
        while( count < 2 ){ 
            if( count == 0) {
                width = Integer.parseInt( number);
                count++;
                break;
            }
            else if (count == 1) {
                height = Integer.parseInt( number);
                count++;
                break;
            }
        }
    } 
    System.out.println( width * height );  
}

This is how the user has to use the program at the moment 这是用户目前必须使用该程序的方式

  1. Enter number 1 and press enter 输入数字1,然后按Enter
  2. Enter number 2 and press enter 输入数字2并按Enter
  3. Enter nothing and press enter 不输入任何内容,然后按Enter
  4. The program prints the multiplied numbers 程序打印出相乘的数字

But this is how the user should use the program at the moment: 但这是用户此时应使用该程序的方式:

  1. Enter number 1 and press enter 输入数字1,然后按Enter
  2. Enter number 2 and press enter 输入数字2并按Enter
  3. The program prints the multiplied numbers 程序打印出相乘的数字

Of course my program has to do something different for the assignment but I have changed it a little bit to make it easier to explain here. 当然,我的程序必须对赋值做一些不同的事情,但是我对其进行了一些更改,以使其在此处易于解释。

Thank you for you help in advance! 谢谢您的提前帮助!

Since you're doing a school assignment, I'll make another suggestion: eliminate the confusing assignment-within-a-condition. 由于您正在做学校作业,因此我将提出另一个建议:消除条件内令人困惑的作业。 I know you've seen this somewhere, and will continue to see it in a lot of places, and will even run into people who advocate it passionately, but I think it tends to confuse things. 我知道您已经在某个地方看到了它,并将继续在很多地方看到它,甚至会遇到热情地拥护它的人,但是我认为它会使事情变得混乱。 How about: 怎么样:

for (int i=0; i<2; i++)
{
  String number = reader.readLine();
  if (i == 0) { height = Integer.parseInt(number); }
         else { width = Integer.parseInt(number); }
}

Do that count < 2 check before the readLine(), otherwise it will try to read a number before it checks the count. 在readLine()之前进行count <2检查,否则在检查计数之前尝试读取一个数字。

Ie those checks are evaluated left to right 即这些检查是从左到右评估的

Try this modifications: 试试这个修改:

public static void main(String[] args) throws NumberFormatException,
        IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(
            System.in));
    int count = 0;
    int width = 0;
    int height = 0;
    String number;
    while (count < 2) { // Just 2 inputs
        number = reader.readLine();
        if (count == 0) {
            width = Integer.parseInt(number);
            count++;
        } else if (count == 1) {
            height = Integer.parseInt(number);
            count++;
        }
        else // If count >= 2, exits while loop
            break;
    }
    System.out.println(width * height);
}

use java.util.Scanner class for user input 使用java.util.Scanner类进行用户输入

Scanner scanner = new Scanner(System.in);
int width = scanner.nextInt();
int height = scanner.nextInt();
scanner.close();
System.out.println( width * height ); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM