首页 > 解决方案 > Try and catch 语句不会让我再试一次

问题描述

我不知道为什么 try catch 语句不起作用:

import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {
    protected static int intNum;
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        while(true) {
            System.out.println("[Shopping List] This is the shopping list GUI by Endercy.");
            System.out.println("[Shopping List] Controls are here:");
            System.out.println("[Shopping List] Type 1 to create a Shopping List");
            System.out.println("[Shopping List] Type 2 to enter a Shopping List name to view its contents");
            System.out.println("[Shopping List] Type 3 Add an item to your shopping list");
            System.out.println("[Shopping List] Type 4 to remove an item from your shopping list");
            System.out.println("[Shopping List] Type 5 to add a price to one of your items in your shopping list");
            intNum = scanner.nextInt();
            switch(intNum) {
                case 1:
                    System.out.println("test");
                    break;
                case 2:
                    System.out.println("test test");
                    break;
                case 3:
                    System.out.println("test test test");
                    break;
                case 4:
                    System.out.println("test test test test");
                    break;
                case 5:
                    System.out.println("test test test test test");
                    break;
                default:
                    while(true) {
                        System.out.println("I'm sorry, but this number is not on our controls list. Try again!");
                            try {
                                intNum = scanner.nextInt();
                            } catch (InputMismatchException e) {
                                System.out.println("Invalid Number, try again!");
                                intNum = scanner.nextInt();
                            }
                        switch(intNum) {
                            case 1:
                                System.out.println("test");
                                break;
                            case 2:
                                System.out.println("test test");
                                break;
                            case 3:
                                System.out.println("test test test");
                                break;
                            case 4:
                                System.out.println("test test test test");
                                break;
                            case 5:
                                System.out.println("test test test test test");
                                break;
                        }
                    }
            }
            break;
        }
        System.out.println("Thanks for trying out this thingy");
    }
}

这是代码,我只想做 InputMismatchException 以防有人输入无效的东西,如 283479234729472983472984723 或字符串等。

但是当我运行它时,我输入了类似的东西它不允许我用扫描仪再次尝试它只是跳到:

Exception in thread "main" java.util.InputMismatchException: For input string: "879857294729847917412942472394724237492734923"
    at java.base/java.util.Scanner.nextInt(Scanner.java:2264)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at com.endercy.challenges.java.arraylist.shoppinglist.Main.main(Main.java:19)

它不让我进入另一个。

标签: javaexception

解决方案


您应该在第一个scanner.nextInt 方法中放置另一个try catch 块。我认为代码没有通过那里并在那里引发异常


推荐阅读