java - 我如何返回到之前的菜单?

标签 java while-loop switch-statement

我已经查看了与此相关的所有内容,如果我进行 do/while 循环,它只会重复选择。如果我将它们设为条件而不是开关,则会给出“NoSuchElementException:未找到行”。现在它也给了我一个“NoSuchElementException:找不到行”,即使我回到了开关。我只是想知道这段代码中缺少什么,可以让用户退出他们的第一个选择(while 循环)以进行不同的选择。这是代码:

public class Zoo {
    static FileRead fr = new FileRead();
    private static final Scanner scnr = new Scanner(System.in);

    public static void main(String[] args) throws FileNotFoundException {
        while (true) {
            int userChoice = menu();
            while (userChoice == 1) {
                // Select Animal
                int animal = animalSelect();
                String Name = null;
                    switch (animal) {
                        case 1:
                            Name = "Animal - Lion";
                            break;
                        case 2:
                            Name = "Animal - Tiger";
                            break;
                        case 3:
                            Name = "Animal - Bear";
                            break;
                        case 4:
                            Name = "Animal - Giraffe";
                            break;
                        default:
                            userChoice = menu();
                            break;
                    } FileRead.readAnimal(Name);
            }

            while (userChoice == 2) {
                // Select Habitat
                int animal = habitatSelect();
                String Name = null;
                    switch (animal) {
                        case 1:
                            Name = "Habitat - Penguin";
                            break;
                        case 2:
                            Name = "Habitat - Bird";
                            break;
                        case 3:
                            Name = "Habitat - Aquarium";
                            break;
                        default:
                            userChoice = menu();
                            break;
                        }
                    FileRead.readHabitat(Name);
               }

            // Exit Program
            if (userChoice == 3) {
                System.out.println("Thank you!");
                System.exit(0);
                }

            // Error for invalid option
            else {
                System.out.println("ERROR: Invalid Selection");
                }
            }
        }

    private static int habitatSelect() {
        // Habitat Menu
        System.out.println("Which habitat would you like to monitor?");
        System.out.println("1. Penguin Habitat");
        System.out.println("2. Bird Habitat");
        System.out.println("3. Aquarium");
        System.out.println("4. Exit");
        int userChoice = Integer.parseInt(scnr.nextLine());
        return userChoice;
    }

    private static int animalSelect() {
        // Animal Menu
        System.out.println("Which animal would you like to monitor?");
        System.out.println("1. Lion");
        System.out.println("2. Tiger");
        System.out.println("3. Bear");
        System.out.println("4. Giraffe");
        System.out.println("5. Exit");
        int userChoice = Integer.parseInt(scnr.nextLine());
        return userChoice;
    }

    private static int menu() {
        // Main Menu
        System.out.println("WELCOME! Plese choose from the following");
        System.out.println("1. Monitor Animal");
        System.out.println("2. Monitor Habitat");
        System.out.println("3. Exit");
        int userChoice = Integer.parseInt(scnr.nextLine());
        return userChoice;
    }
}

这一切都从包中的另一个文件读取。如果需要该代码我也会发布它。

最佳答案

调整你的主要方法如下

public static void main(String[] args) throws FileNotFoundException {
  while (true) {
     int userChoice = menu();

     switch (userChoice) {
     case 1: // only for animals
        int animal = animalSelect();
        String Name = null;
        switch (animal) {
        case 1:
           Name = "Animal - Lion";
           break;
        case 2:
           Name = "Animal - Tiger";
           break;
        case 3:
           Name = "Animal - Bear";
           break;
        case 4:
           Name = "Animal - Giraffe";
           break;
        default:
           System.out.println("ERROR: Invalid Selection");
           break;
        } 
        if (Name != null)  // read file only if selection is correct
           FileReader.readAnimal(Name);
        break;

     case 2: // only for habitat
        int habitat = habitatSelect();
        String habitatName = null;
        switch (habitat) {
        case 1:
           habitatName = "Habitat - Penguin";
           break;
        case 2:
           habitatName = "Habitat - Bird";
           break;
        case 3:
           habitatName = "Habitat - Aquarium";
           break;
        default:
           System.out.println("ERROR: Invalid Selection");
           break;
        } 
        if (habitatName != null) // read file only if selection is correct
           FileRead.readHabitat(habitatName);
        break;

     case 3 : // only for exit
        System.out.println("Thank you!");
        System.exit(0);

     default:
        System.out.println("ERROR: Invalid Selection");
     }

  }
}

因此,在每个子菜单之后,用户都会返回到主菜单。至于您的异常(exception)情况,目前我添加了空检查,以便仅在选择正确时才读取文件。

另外,请注意,上面的代码不包含嵌套循环,这会提高性能,并且还排除(稍微困惑的)递归调用。

关于java - 我如何返回到之前的菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51881172/

相关文章:

c - 如何使用带命令行参数的嵌套 Switch?

java - 如果输入不正确,如何重新开始 switch 语句

java - Kafka通过微服务,订阅不存在的主题

java - While 循环不重复

java - 使用 EventKey 在 EditText 上设置文本

python - 变量初始化时收到 "UnboundLocalError: local variable ' e'赋值前引用"

php - 如何在 PHP 中使用 while 循环按 ID 列出节中的数据?

java switch 圈复杂度与性能

java - 在运行时更新 JAR

java - 使用 Gradle 和 Spring Boot 从 Java 引用 Groovy 导致“找不到符号”