gpt4 book ai didi

java - 如何重新提示无效 boolean 输入(java)

转载 作者:行者123 更新时间:2023-12-01 22:35:07 25 4
gpt4 key购买 nike

当我运行程序时,如果我输入“true”或“false”以外的内容,它会抛出 InputMismatchException。

do {
System.out.print("Do passengers have an individual tv screen?"
+ "(true OR false): ");
hasVideo = keyboard.nextBoolean();
bus.setIndividualVideo(hasVideo);
} while (!(hasVideo == true) && !(hasVideo == false));

最佳答案

捕获错误并将其视为无效响应...

try {
System.out.print("Do passengers have an individual tv screen?"
+ "(true OR false): ");
hasVideo = keyboard.nextBoolean();
} catch (InputMismatchException exp) {
System.err.println("Please, enter only true or false");
}

看看The try Block了解更多详情

关于java - 如何重新提示无效 boolean 输入(java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26984660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com