You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Java中实现执行操作后返回主菜单的循环方法

Fixing Your Java Menu Loop Issue

Got it, let's get your math program looping back to the menu after each operation! The core problem here is that your current code runs once and exits—we just need to wrap your menu and selection logic in a loop so it repeats until the user chooses to quit.

Here's a polished, working version of your code with the loop added, plus some quality-of-life improvements:

import java.util.Scanner;

public class MathProgram {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        boolean keepProgramRunning = true;

        // do-while loop ensures the menu runs at least once
        do {
            // Print your menu
            System.out.println("Hello, This is the math program");
            System.out.println("Select an application from below: \n");
            System.out.println("(1) Pythagorean Theorem");
            System.out.println("(2) Subtraction");
            System.out.println("(3) Multiplication");
            System.out.println("(4) Division");
            System.out.println("(5) Exit");
            System.out.print("\nEnter your choice: ");

            // Get user's selection
            int userChoice = scanner.nextInt();
            scanner.nextLine(); // Clear the leftover newline from the input buffer

            // Handle each selection with a switch statement
            switch (userChoice) {
                case 1:
                    System.out.println("\nRunning Pythagorean Theorem calculation...");
                    // Paste your existing Pythagorean code here
                    break;
                case 2:
                    System.out.println("\nRunning Subtraction...");
                    // Paste your existing Subtraction code here
                    break;
                case 3:
                    System.out.println("\nRunning Multiplication...");
                    // Paste your existing Multiplication code here
                    break;
                case 4:
                    System.out.println("\nRunning Division...");
                    // Paste your existing Division code here
                    break;
                case 5:
                    System.out.println("\nExiting program. Goodbye!");
                    keepProgramRunning = false; // This tells the loop to stop
                    break;
                default:
                    System.out.println("\nInvalid choice! Please pick a number between 1 and 5.\n");
            }

            // If the user isn't exiting, pause before returning to the menu
            if (keepProgramRunning) {
                System.out.println("\nPress Enter to go back to the menu...");
                scanner.nextLine();
                System.out.println("\n----------------------------------------\n");
            }

        } while (keepProgramRunning); // Loop until user chooses to exit

        scanner.close();
    }
}

Key Changes Explained:

  • do-while Loop: This type of loop runs first, then checks if it should repeat. Perfect for menus because you want the menu to show up immediately, not wait for a condition first.
  • Exit Option (5): Gives users a clean way to quit the program instead of having to force-close it.
  • Input Buffer Cleanup: The scanner.nextLine() after nextInt() fixes a common bug where leftover newline characters cause unexpected input behavior later.
  • User Experience Touches: The pause and separator line make it easier for users to follow along—they can read the result of their operation before jumping back to the menu.

Just drop your existing code for each math operation into the corresponding case block, and you're good to go!

内容的提问来源于stack exchange,提问作者TheAuraKing

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:24:06