java.util.NoSuchElementException:未找到行,是否需要处理此异常?

java.util.NoSuchElementException:未找到行,是否需要处理此异常?,java,Java,我的程序可以编译,但我遇到运行时异常,不确定是否需要处理它们?方法被注释掉,对于扩展类,它们只显示文本。我也不知道我是否正确使用了标签?我希望我的程序能够循环并处理错误字符的异常,但只有在用户输入“5”时才会终止 您的程序存在多个问题: 您定义了shouldExit,但没有根据用户输入更新它 可以在main()和menu()中扫描和处理用户输入。通常,您的代码不是以连贯可读的方式构造的,并且很少重用 println()已打印新行(与print()相反) 出于对编程女神的热爱-不要使用System

我的程序可以编译,但我遇到运行时异常,不确定是否需要处理它们?方法被注释掉,对于扩展类,它们只显示文本。我也不知道我是否正确使用了标签?我希望我的程序能够循环并处理错误字符的异常,但只有在用户输入“5”时才会终止


您的程序存在多个问题:

  • 您定义了shouldExit,但没有根据用户输入更新它
  • 可以在main()和menu()中扫描和处理用户输入。通常,您的代码不是以连贯可读的方式构造的,并且很少重用
  • println()
    已打印新行(与
    print()
    相反)
  • 出于对编程女神的热爱-不要使用System.exit()终止Java程序
  • 以下是一个工人阶级的例子,他们将上述指导方针实施到了可接受的程度:

    import java.util.*;
    
    public class Mainmenu3
    {
        static Scanner in;
    
        public static void main(String args[])
        {
            in = new Scanner(System.in);
            boolean shouldExit = false;
            while (!shouldExit) {
                displayMenu();
                shouldExit = processUserInput();
            }
        }
    
        public static void displayMenu()
        {
            System.out.println();
            System.out.println("Created by Darren Estcourt");
            System.out.println("Please choose an option : ");
            System.out.println("1. Choose team");
            System.out.println("2. Create profile");
            System.out.println("3. Load/Save game");
            System.out.println("4. Credits");
            System.out.println("5. Quit");
        }
    
        public static int getUserInput()
        {
            int option = -1;
            try {
                option = in.nextInt();
            } catch (InputMismatchException e) {
                // When a scanner throws an InputMismatchException, 
                // the scanner will not pass the token that caused the exception,
                // so that it may be retrieved or skipped via some other method.
                in.next();
            } catch (NoSuchElementException e) {
                // input is exhausted
                option = 5;
            } catch (IllegalStateException e) {}
                // scanner is closed
                option = 5;
            }
            return option;
        }
    
        public static boolean processUserInput()
        {
            int option = getUserInput();
            switch (option) {
            case 1:
                chooseTeam();
                break;
            case 2:
                createProfile();
                break;
            case 3:
                loadSave();
                break;
            case 4:
                credits();
                break;
            case 5:
                System.out.println("Goodbye!");
                return true;
            default:
                System.out.println("Invalid choice");
            }
            return false;
        }
    
        public static void chooseTeam()
        {
            System.out.println();
            System.out.println("Select Team : ");
            System.out.println("1. Arsenal");
            System.out.println("2. Aston Villa");
            System.out.println("3. Bournemouth");
            System.out.println("4. Chelsea");
            System.out.println("5. Crystal Palace");
            System.out.println("6. Everton");
            System.out.println("7. Leicester City");
            System.out.println("8. Liverpool");
            System.out.println("9. Manchester United");
            System.out.println("10. Manchester City");
            System.out.println("11. Newcastle United");
            System.out.println("12. Norwich City");
            System.out.println("13. Southampton");
            System.out.println("14. Stoke City");
            System.out.println("15. Sunderland");
            System.out.println("16. Swansea City");
            System.out.println("17. Tottenham Hotspur");
            System.out.println("18. Watford");
            System.out.println("19. West Brom");
            System.out.println("20. West Ham United");
    
            int option = getUserInput();
    
            System.out.println("You entered : " + option);
        } // end chooseTeam
    
        public static void createProfile()
        {
            System.out.println("createProfile");
        }
    
        public static void loadSave()
        {
            System.out.println("loadSave");
        }
    
        public static void credits()
        {
            System.out.println("credits");
        }
    }
    

    请提供一个stacktrace,如果它们不相关,请删除注释代码。我想处理InputMismatchException。如果用户输入一个字符,这个程序(sharonbn解决方案)将进入一个永久循环!还有其他人有解决办法吗?所需要的只是再多做一点研究,以找出原因。我修复了答案中的
    getUserInput()
    方法(现在您可以看到重用代码有什么好处:)
    import java.util.*;
    
    public class Mainmenu3
    {
        static Scanner in;
    
        public static void main(String args[])
        {
            in = new Scanner(System.in);
            boolean shouldExit = false;
            while (!shouldExit) {
                displayMenu();
                shouldExit = processUserInput();
            }
        }
    
        public static void displayMenu()
        {
            System.out.println();
            System.out.println("Created by Darren Estcourt");
            System.out.println("Please choose an option : ");
            System.out.println("1. Choose team");
            System.out.println("2. Create profile");
            System.out.println("3. Load/Save game");
            System.out.println("4. Credits");
            System.out.println("5. Quit");
        }
    
        public static int getUserInput()
        {
            int option = -1;
            try {
                option = in.nextInt();
            } catch (InputMismatchException e) {
                // When a scanner throws an InputMismatchException, 
                // the scanner will not pass the token that caused the exception,
                // so that it may be retrieved or skipped via some other method.
                in.next();
            } catch (NoSuchElementException e) {
                // input is exhausted
                option = 5;
            } catch (IllegalStateException e) {}
                // scanner is closed
                option = 5;
            }
            return option;
        }
    
        public static boolean processUserInput()
        {
            int option = getUserInput();
            switch (option) {
            case 1:
                chooseTeam();
                break;
            case 2:
                createProfile();
                break;
            case 3:
                loadSave();
                break;
            case 4:
                credits();
                break;
            case 5:
                System.out.println("Goodbye!");
                return true;
            default:
                System.out.println("Invalid choice");
            }
            return false;
        }
    
        public static void chooseTeam()
        {
            System.out.println();
            System.out.println("Select Team : ");
            System.out.println("1. Arsenal");
            System.out.println("2. Aston Villa");
            System.out.println("3. Bournemouth");
            System.out.println("4. Chelsea");
            System.out.println("5. Crystal Palace");
            System.out.println("6. Everton");
            System.out.println("7. Leicester City");
            System.out.println("8. Liverpool");
            System.out.println("9. Manchester United");
            System.out.println("10. Manchester City");
            System.out.println("11. Newcastle United");
            System.out.println("12. Norwich City");
            System.out.println("13. Southampton");
            System.out.println("14. Stoke City");
            System.out.println("15. Sunderland");
            System.out.println("16. Swansea City");
            System.out.println("17. Tottenham Hotspur");
            System.out.println("18. Watford");
            System.out.println("19. West Brom");
            System.out.println("20. West Ham United");
    
            int option = getUserInput();
    
            System.out.println("You entered : " + option);
        } // end chooseTeam
    
        public static void createProfile()
        {
            System.out.println("createProfile");
        }
    
        public static void loadSave()
        {
            System.out.println("loadSave");
        }
    
        public static void credits()
        {
            System.out.println("credits");
        }
    }