首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >java do while循环如何与try catch转到无穷大?

java do while循环如何与try catch转到无穷大?
EN

Stack Overflow用户
提问于 2018-08-17 01:15:24
回答 1查看 0关注 0票数 0

以下是我使用的代码:

代码语言:javascript
复制
do {
        try {
                System.out.println("Please enter the type of employee. (1 - salaried, 2 - hourly, 3 - commissioned)");
                type = user_input.nextInt();
                letter = true;
            }
            catch (InputMismatchException e) {
                letter = false;
                System.out.print("Entered value is not an integer. Try again.");
            }
        } while (!letter);
EN

回答 1

Stack Overflow用户

发布于 2018-08-17 10:22:33

解决方案:

代码语言:txt
复制
String type = null;
Scanner user_input = new Scanner(System.in);
boolean letter = true;
do {
    try {
        System.out.println("Please enter the type of employee. (1 - salaried, 2 - hourly, 3 - commissioned)");
        type = user_input.nextLine();
        Integer.valueOf(type);
    } catch (Throwable e) {
        letter = false;
        System.out.print("Entered value is not an integer. Try again.");
    }
} while (letter);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100002228

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档