Java-不兼容类型:无法将布尔值转换为int
而不是使用Java-不兼容类型:无法将布尔值转换为int,java,Java,而不是使用reader.hasNextInt()应该使用reader.nextInt() 但请记住,此方法会引发3种类型的错误,应予以处理: 输入不匹配异常-如果下一个令牌与 整数正则表达式,或超出范围 NoTouchElementException-如果输入用尽 非法状态异常-如果此扫描仪已关闭 reader.hasNextInt()返回一个布尔值reader.nextInt()hasNextInt()告诉您是否有要读取的intnextInt实际上检索令牌。潜在的InputMismatchE
reader.hasNextInt()
应该使用reader.nextInt()代码>
但请记住,此方法会引发3种类型的错误,应予以处理:
输入不匹配异常
-如果下一个令牌与
整数正则表达式,或超出范围
NoTouchElementException
-如果输入用尽
非法状态异常
-如果此扫描仪已关闭
reader.hasNextInt()
返回一个布尔值reader.nextInt()
hasNextInt()
告诉您是否有要读取的intnextInt
实际上检索令牌。潜在的InputMismatchException谢谢,这会使程序运行,但我确实得到了InputMismatchException和NoTouchElementException。您对如何处理这些问题有什么建议吗?您可以使用标准的try-catch块来处理它
import java.util.Scanner;
import java.lang.Object;
public class Game {
public static void main(String[] args) {
int numberOfRounds = -1;
while (numberOfRounds <= -1) {
Scanner reader = new Scanner(System.in);
System.out.print("Please input the number of rounds. The number of rounds must be greater than or equal to 0: ");
numberOfRounds = reader.hasNextInt();
reader.close();
}
System.out.println("number is" + numberOfRounds);
}
}
Game.java:11: error: incompatible types: boolean cannot be converted to int
numberOfRounds = reader.hasNextInt();
^
1 error