gpt4 book ai didi

java - System.in.read()方法

转载 作者:行者123 更新时间:2023-12-01 07:06:29 24 4
gpt4 key购买 nike

以下是我编写的代码,用于从用户那里获取两个输入。但是当我运行该程序时,它只需要一个输入并自行生成其他输入并计算出错误的值。请帮忙。谢谢

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 作为输入):

 Scanner sc = new Scanner(System.in);
int i = sc.nextInt();

更多示例: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html

关于java - System.in.read()方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22708071/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com