猿问

我在 Java 中使用 while 循环、if_else 和 try_catch 与扫描器语句并

尝试接受 [0,1,2,3,4] 范围内的用户输入,直到他输入有效数字。使用try-catch修复异常错误


   boolean good = true;

   int x = reader.nextInt();

    while (good != false) {

    try {

        if (x > launchEventList.size()-1) {

            System.out.println("Please provide a valid input");

            reader.nextInt();

        } else {  

            System.out.println("\nDetails of event on index "+ x + " are ==>> " + getLe(x));

            good = false;

        }

    }

    catch (IndexOutOfBoundsException e) {

        System.out.println("Invalid Input");

    }


开满天机
浏览 62回答 1
1回答

慕工程0101907

在 if 语句中存储reader.nextInt()变量 x。x=reader.nextInt();
随时随地看视频慕课网APP

相关分类

Java
我要回答