我们的教授让我们使用Java做一些基本编程,他提供了一个网站让我们注册并提交问题。今天我需要完成一个例子,感觉自己走在正确的轨道上,但我只是无法解决剩下的部分。以下是实际的问题:
**Sample Input:**
10 12
10 14
100 200
**Sample Output:**
2
4
100
下面是我目前为止得到的内容:
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]));
}
}
现在我总是得到答案为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.
无论我使用哪个答案版本,我都会得到这个错误,我该怎么办?但是,如果我在Eclipse中运行它,选择“运行配置”>“程序参数”。
10 12
10 14
100 200
我没有任何输出
编辑
我已经有了一些进展,最初我遇到了编译错误,然后是运行时错误,现在我得到的是错误的答案,所以有人能帮我看看这个程序哪里出了问题吗:
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]));
}
}
}
}