System.in.read()方法 [英] System.in.read() method
本文介绍了System.in.read()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是代码,我写的是从用户那里得到两个输入。但是当我运行程序时,它只需要一个输入并自己生成另一个并计算错误的值。
请帮忙。谢谢
Following is the code, I have written to get two inputs from user. But when I run the program, It takes only one input and generate other by itself and calculate the wrong value. please help. Thanks
import java.io.IOException;
import java.util.*;
class ThrowsExcpt {
int divide(int x, int y) throws ArithmeticException, IOException {
return x / y;
}
}
class ThrowsTemps {
public static void main(String s[]) throws IOException {
int x = System.in.read();
int y = System.in.read();
ThrowsExcpt th = new ThrowsExcpt();
int r = th.divide(x, y);
System.out.println(r);
}
}
推荐答案
System.in
是 InputStream
- read()
完全读取一个字节。您的直接输入是多个字节,因此在第一个输入中直接读取这两个值。
System.in
is an InputStream
- read()
reads exactly one byte. Your direct input is more than one byte and so both values are directly read within the first input.
使用扫描器(使用System.in作为输入):
Use a Scanner instead (with System.in as Input):
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
更多示例:
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/ Scanner.html
这篇关于System.in.read()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文