如何在Java中读取多行输入

Gan*_*row 17 java

我们的教授正在让我们用Java做一些基本的编程,他给了一个网站以及注册和提交问题的一切,今天我需要做一个例子我觉得我走在正确的轨道但我不能弄明白其余的.这是实际问题:

**Sample Input:**
10 12
10 14
100 200

**Sample Output:**
2
4
100
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止所得到的:

public class Practice {

    public static int calculateAnswer(String a, String b) {
        return (Integer.parseInt(b) - Integer.parseInt(a));
    }

    public static void main(String[] args) {
        System.out.println(calculateAnswer(args[0], args[1]));
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我总能得到答案,2因为我正在阅读单行,我怎样才能考虑所有行?谢谢

由于某些奇怪的原因,每次我想执行时都会收到此错误:

C:\sonic>java Practice.class 10 12
Exception in thread "main" java.lang.NoClassDefFoundError: Fact
Caused by: java.lang.ClassNotFoundException: Fact.class
        at java.net.URLClassLoader$1.run(URLClassLoader.java:20
        at java.security.AccessController.doPrivileged(Native M
        at java.net.URLClassLoader.findClass(URLClassLoader.jav
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248
Could not find the main class: Practice.class.  Program will exit.
Run Code Online (Sandbox Code Playgroud)

无论我使用什么版本的答案,我都会收到此错误,我该怎么办?

但是如果我在eclipse中运行它运行>运行配置 - >程序参数

10 12
10 14
100 200
Run Code Online (Sandbox Code Playgroud)

我没有输出

编辑

我已经取得了一些进展,起初我得到了编译错误,然后是运行时错误,现在我得到了错误的答案,所以任何人都可以帮我解决这个问题:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;

public class Practice {

    public static BigInteger calculateAnswer(String a, String b) {
        BigInteger ab = new BigInteger(a);
        BigInteger bc = new BigInteger(b);
        return bc.subtract(ab);
    }

    public static void main(String[] args) throws IOException {
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); 
        String line; 

        while ((line = stdin.readLine()) != null && line.length()!= 0) { 
            String[] input = line.split(" "); 
            if (input.length == 2) { 
                System.out.println(calculateAnswer(input[0], input[1])); 
            } 
        } 
    }
}
Run Code Online (Sandbox Code Playgroud)

Gan*_*row 19

我终于得到了它,无论出于何种原因,它被拒绝了13次,第14次"法官"接受了我的回答,这里是:

import java.io.BufferedInputStream;
import java.util.Scanner;

public class HashmatWarrior {

    public static void main(String args[]) {
        Scanner stdin = new Scanner(new BufferedInputStream(System.in));
        while (stdin.hasNext()) {
            System.out.println(Math.abs(stdin.nextLong() - stdin.nextLong()));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Pét*_*rök 8

使用BufferedReader,你可以从标准输入读取如下:

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String line;

while ((line = stdin.readLine()) != null && line.length()!= 0) {
    String[] input = line.split(" ");
    if (input.length == 2) {
        System.out.println(calculateAnswer(input[0], input[1]));
    }
}
Run Code Online (Sandbox Code Playgroud)