如何返回上一个菜单?

时间:2018-08-16 16:02:08

标签: 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;
    }
}

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

1 个答案:

答案 0 :(得分:0)

如下调整您的主要方法

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");
     }

  }
}

因此,在每个子菜单之后,用户将返回到主菜单。至于您的例外情况,目前我添加了一个空检查,以便仅在选择正确的情况下才读取该文件。

另外,请注意,上面的代码不包含嵌套循环,该嵌套循环可以提高性能,并且不包括(稍微凌乱的)递归调用。