如何在Java中读取多行输入 [英] How to read input with multiple lines in Java
问题描述
我们的教授正在使我们使用Java进行一些基本编程,他提供了一个网站以及一切用于注册和提交我们的问题的方法,今天我需要做一个例子,我觉得自己步入正轨,但我只是无法弄清楚其余的.这是实际的问题:
Our professor is making us do some basic programming with Java, he gave a website and everything to register and submit our questions, for today I need to do this one example I feel like I'm on the right track but I just can't figure out the rest. Here is the actual question:
**Sample Input:**
10 12
10 14
100 200
**Sample Output:**
2
4
100
这是到目前为止我得到的:
And here is what I've got so far :
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
,因为我正在阅读单行,如何考虑所有行?谢谢
Now I always get the answer 2
because I'm reading the single line, how can I take all lines into account? thank you
由于某些奇怪的原因,每次我要执行时都会收到此错误:
For some strange reason every time I want to execute I get this error:
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.
无论使用什么版本的答案,我都会收到此错误,该怎么办?
Whatever version of answer I use I get this error, what do I do ?
但是,如果我在eclipse中运行它,则运行方式>运行配置->程序参数
However if I run it in eclipse Run as > Run Configuration -> Program arguments
10 12
10 14
100 200
我没有输出
编辑
我已经取得了一些进展,起初我遇到了编译错误,然后是运行时错误,现在我得到了错误的答案,所以任何人都可以帮我解决一下这是怎么回事:
I have made some progress, at first I was getting the compilation error, then runtime error and now I get wrong answer, so can anybody help me what is wrong with this:
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]));
}
}
}
}
推荐答案
我终于得到了它,无论出于何种原因都被提交了13次,第14位法官"接受了我的回答,这里是:
I finally got it, submited it 13 times rejected for whatever reasons, 14th "the judge" accepted my answer, here it is :
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()));
}
}
}
这篇关于如何在Java中读取多行输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!