【问题标题】:Where to put the try catch block在哪里放置 try catch 块
【发布时间】:2022-01-13 06:59:12
【问题描述】:

大家早上好...我想知道在此代码中将非整数 Input(InputMismatchException) 的 try-catch 块放在哪里工作, 如果我把它放在这里不起作用,如果我把它放在while之前,它是一个无限循环,如果你能解释它在哪里以及为什么它会完美,谢谢你的时间。

public void additionalIngredients() {
    Set<Integer> set = new HashSet<>();
    boolean flag = false;
    while (!flag) {
        System.out.println("Enter your choice for extra toppings ");
        int choices = scanner.nextInt();
        scanner.nextLine();
        try {
            switch (choices) {
                case 0:
                    System.out.println("Done");
                    flag = true;
                    break;
                case 1:
                    if (!set.contains(choices)) {
                        double extraSauce = 1.2;
                        setAdditionalStuff(getAdditionalStuff() + extraSauce);
                        System.out.println("Extra sauce added on your pizza \n");
                        break;
                    } else {
                        System.out.println("You already added extra sauce, please consider to add something else");
                        break;
                    }
                case 2:
                    if (!set.contains(choices)) {
                        double extraCheese = 2.3;
                        setAdditionalStuff(getAdditionalStuff() + extraCheese);
                        System.out.println("Extra cheese added on your pizza \n");
                        break;
                    } else {
                        System.out.println("You already added extra cheese, please consider to add something else");
                        break;
                    }
                case 3:
                    if (!set.contains(choices)) {
                        double largeDough = 0.7;
                        setAdditionalStuff(getAdditionalStuff() + largeDough);
                        System.out.println("Your pizza has a large Dough \n");
                        break;
                    } else {
                        System.out.println("Your pizza has already a large dough, consider something else");
                        break;
                    }
            }

            set.add(choices);
        } catch (InputMismatchException e) {
            System.out.println("Please enter an integer!");
        }
    }
}

【问题讨论】:

  • 你应该在try块try{ int choices = scanner.nextInt(); ...}中包含从用户那里获取输入
  • 仍然是无限循环,我想我必须从一开始就检查我的输入是否为整数,这家伙说的话,使用另一种方法。
  • 在 catch 块中包含您的 scanner.nextLine();

标签: java switch-statement try-catch hashset


【解决方案1】:

有很多方法可以解决这个问题。始终努力寻求使您的代码更易于阅读和理解的解决方案。

一种方法:例如,您可以创建另一个辅助方法fetchSelectionFromUser(),它只做一个的事情:循环(使用try catch)直到用户输入一个有效强>数字。

一个指导原则是single layer of abstraction 原则。这告诉你,在循环中尝试切换不是要走的路。如前所述,您可以选择一个简单地调用两个方法的循环:一个获取输入,另一个处理它。

【讨论】:

  • uuu 知道了,谢谢,我要试试。
  • @Florin123 不客气。请考虑在某个时候点赞和/或接受,以防您觉得它“足够”有帮助;-)
猜你喜欢
  • 2016-07-02
  • 1970-01-01
  • 2010-10-06
  • 1970-01-01
  • 2021-01-27
  • 1970-01-01
  • 2012-11-29
  • 1970-01-01
  • 2015-02-08
相关资源
最近更新 更多