Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-不兼容类型:无法将布尔值转换为int_Java - Fatal编程技术网

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()
告诉您是否有要读取的int
nextInt
实际上检索令牌。潜在的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