切换大小写选择 Java 后如何恢复到菜单

问题描述 投票:0回答:1

我正在创建一个管理程序,我需要在其中显示一个菜单,其中包含供用户选择的选项。例如,当用户选择案例1时,我想显示一些输出,然后自动返回主菜单以完成更多案例。但是,每次我选择一个案例时,都会打印输出,但菜单永远不会再次打印。除非我手动这样做,否则该程序也永远不会终止。我想也许存在无限循环错误,所以我在用户选择 1 时运行的方法下放置了一条调试语句“End of list”。在输出中,这一行也永远不会出现。

这是我的代码:

public static void main(String[] args) {
    TripManagement tm = new TripManagement();

    // Load data into containers
    tm.loadEmployees();
    tm.loadTrucks();
    tm.loadTrips();

    Scanner scanner = new Scanner(System.in);
    
    boolean more = true;
    int choice = -1;
    
    while(more) {
        
        try {
            // Display the main menu
            System.out.println("-----------------------------------------");
            System.out.println("1. Display all employees");
            System.out.println("2. Display all trucks");
            System.out.println("3. Display all trips");
            System.out.println("4. Find an employee");
            System.out.println("5. Find a truck");
            System.out.println("6. Find a trip");
            System.out.println("7. Add a new employee");
            System.out.println("8. Add a new truck");
            System.out.println("9. Add a new trip");
            System.out.println("10. Save all data into files");
            System.out.println("0. Exit");
            System.out.println("Enter a choice: ");
            
            // Get user choice
            if (scanner.hasNextInt()) {
                choice = scanner.nextInt();
                scanner.nextLine(); // Consume the newline character
            } else {
                // Clear the input buffer
                scanner.nextLine();
                System.out.println("Wrong input. Please enter a valid number.");
                continue;
            }
     
            // Handle user choice
            switch (choice) {
                case 0: 
                    System.out.println("Bye!");
                    more = false;
                    break;
                case 1:
                    tm.outputEmployees();
                    System.out.println("End of list");
                    break;
                case 2:
                    tm.outputTrucks();
                    break;
                case 3:
                    tm.outputTrips();
                    break;
                case 4:
                    // Implement logic for finding an employee
                    break;
                case 5:
                    // Implement logic for finding a truck
                    break;
                case 6:
                    // Implement logic for finding a trip
                    break;
                case 7:
                    // Implement logic for adding a new employee
                    break;
                case 8:
                    // Implement logic for adding a new truck
                    break;
                case 9:
                    // Implement logic for adding a new trip
                    break;
                case 10:
                    // Implement logic for saving data into files
                    break;
                default:
                    System.out.println("Invalid choice. Please select a valid option.");
                    break;
            }
            
        } catch(InputMismatchException e) {
            System.out.println("Wrong input type." + e);
            
        }
        
    } 
}

public void outputEmployees() {
    try(Formatter formatter = new Formatter(System.out)) {
        for(Employee employee : employees) {
            employee.outputFormatter(formatter);
            formatter.format("%n");
        }
    }
}

这是我选择案例 1 时的输出:

-----------------------------------------
1. Display all employees
2. Display all trucks
3. Display all trips
4. Find an employee
5. Find a truck
6. Find a trip
7. Add a new employee
8. Add a new truck
9. Add a new trip
10. Save all data into files
0. Exit
Enter a choice: 
1
Employee Number 1
Employee Number 2
Employee Number 3
...
java menu switch-statement
1个回答
0
投票

outputEmployees
中的try-with-resources会关闭
System.out
,至少如果
Formatter
正确转发
close()
的话可能会关闭。这可能就是您看不到更多输出的原因。

© www.soinside.com 2019 - 2024. All rights reserved.