如何在Java中使用多行阅读输入
我们的教授正在让我们用Java进行一些基本编程,他给了一个网站和一切注册并提交我们的问题,今天我需要做这个例子,我觉得我喜欢在正确的轨道上,但我无法弄清楚其余部分。下面是实际的问题:如何在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]));
}
}
}
}
我终于得到它,submited它拒绝任何理由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()));
}
}
}
使用扫描仪类是我正在寻找的确切的东西,非常感谢 – 2012-03-29 18:31:11
调查BufferedReader
。如果这不是一般/高级别,我建议阅读I/O tutorial。
@Hank Gay不是来自文件,它会很容易做到它从控制台参数 – 2010-02-19 14:00:11
@Gandalf,你可以用'BufferedReader'来做到这一点。 'BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));' – 2010-02-19 14:07:09
使用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]));
}
}
很多学生练习使用Scanner
,因为它有多种方法来解析号码。我通常只是开始惯用的面向行的过滤器:
import java.io.*;
public class FilterLine {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
String s;
while ((s = in.readLine()) != null) {
System.out.println(s);
}
}
}
您在命令行中运行的问题是,你不把“的.class”类文件之后。
java Practice 10 12
应该工作 - 只要你是什么地方的java可以找到.class文件。
类路径问题是一个完整的故事。如果java仍然抱怨说它找不到你的类,请转到与你的.class文件相同的目录(并且它没有出现你正在使用的包...),并尝试 -
java -cp . Practice 10 12
@Nate我仍然得到相同的错误信息 – 2010-02-19 14:47:22
是在执行'java -cp的相同目录下的Practice.class。练习10 12'来自? – Nate 2010-02-19 16:20:20
import java.util.*;
import java.io.*;
public class Main {
public static void main(String arg[])throws IOException{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
StringTokenizer st;
String entrada = "";
long x=0, y=0;
while((entrada = br.readLine())!=null){
st = new StringTokenizer(entrada," ");
while(st.hasMoreTokens()){
x = Long.parseLong(st.nextToken());
y = Long.parseLong(st.nextToken());
}
System.out.println(x>y ?(x-y)+"":(y-x)+"");
}
}
}
这个解决方案比上面的解决方案更高效,因为它占用了2.128,而这需要1.308秒才能解决问题。
由于这是一个教育活动,没有必要做性能改进。代码可读性>性能! – Kuchi 2015-10-11 22:50:27
package pac001;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Entry_box{
public static final String[] relationship = {"Marrid", "Unmarried"};
public static void main(String[] args)
{
//TAKING USER ID NUMBER
int a = Integer.parseInt(JOptionPane.showInputDialog("Enter ID no: "));
// TAKING INPUT FOR RELATIONSHIP
JFrame frame = new JFrame("Input Dialog Example #3");
String Relationship = (String) JOptionPane.showInputDialog(frame,"Select Your Relationship","Married",
JOptionPane.QUESTION_MESSAGE, null, relationship,relationship[0]);
//PRINTING THE ID NUMBER
System.out.println("ID no: "+a);
// PRINTING RESULT FOR RELATIONSHIP INPUT
System.out.printf("Mariitual Status: %s\n", Relationship);
}
}
public class Sol {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
System.out.println(sc.nextLine());
}
}
}
如果对您的代码的解释会很好,请添加一点。 – Sakuto 2017-05-03 08:25:21
难道你的教授指定他如何希望你获得输入到你的程序?从命令行读取文件,在程序运行时输入到程序中? – Nate 2010-02-19 14:37:23
@Nate umm他给了我们一个网站http://uva.onlinejudge.org,所以我们在那里注册给他我们的用户名,他希望我们每天解决一个问题,这是一件好事..因为我不能得到这个例子在我的电脑上工作,没有任何修饰它的目的。我试图解决的问题是'10055 - Hashmat勇敢的战士'http://acm.uva.es/p/v100/10055.html – 2010-02-19 14:41:35