为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

开满天机 2023-09-27 16:23:39
尝试接受 [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");    }
查看完整描述

1 回答

?
慕工程0101907

TA贡献1887条经验 获得超5个赞

在 if 语句中存储reader.nextInt()变量 x。

x=reader.nextInt();


查看完整回答
反对 回复 2023-09-27
  • 1 回答
  • 0 关注
  • 61 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信