如何修复整数的值而不是抛出异常?

时间:2017-02-03 20:02:50

标签: java exception int floor

我需要使用floor函数来修复用户输入的值,而不是让它抛出异常并停止。可能吗? 如果用户输入8.3;它应该变为8并且不会抛出异常。知道如何执行此操作吗?

我写的部分代码如下:

try{
    int y=0;
    Scanner inputY = new Scanner(System.in);
    System.out.println("Enter an Integer between 1-9");
    y = inputY.nextInt();
    catch(InputMismatchException e){
    return 0;
    }
    finally{
        System.out.println("The input number was not an integer between 1-9");
        System.exit(0);
    }

1 个答案:

答案 0 :(得分:2)

请勿使用mogrifynextInt()并进行从double到int的转换。

nextDouble()
相关问题