以下是代码,我写的是从用户那里得到两个输入.但是当我运行程序时,它只需要一个输入并自己生成另一个并计算错误的值.请帮忙.谢谢
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);
}
}
Run Code Online (Sandbox Code Playgroud)
System.in是InputStream- read()读取正好一个字节.您的直接输入不止一个字节,因此在第一个输入中直接读取这两个值.
改为使用扫描仪(使用System.in作为输入):
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
Run Code Online (Sandbox Code Playgroud)
更多示例:http: //docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html