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

如何实现Java程序完成题目后返回菜单的功能?

How to Return to Menu After Completing Quiz Questions in Java

Hey there! Let's break down your questions and fix up your code to make it work exactly how you want.

1. How to return to the menu after completing several questions?

The core solution here is using a loop structure (like do-while) to wrap your entire quiz flow. Once all questions are finished, the loop will cycle back to your menu/prompt, letting the user choose to retake the quiz or exit.

2. How to auto-return to menu after all questions are completed?

Your original code has some syntax and logical gaps, so let's rewrite it to properly handle the flow: complete all questions → prompt to retake (return to quiz) or exit. Here's the corrected version with clear explanations:

package com.company;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String userChoice;

        // Outer loop controls returning to the quiz start prompt
        do {
            System.out.println("\nAre you ready to take the quiz? Enter \"Y\" to proceed or \"N\" to exit the program:");
            userChoice = input.nextLine().trim();

            if (userChoice.equalsIgnoreCase("Y")) {
                // Block to handle ALL your quiz questions
                System.out.println("\n--- Starting Quiz ---");
                // Replace this sample loop with your actual question logic
                for (int i = 1; i <= 3; i++) {
                    System.out.println("Question " + i + ": [Add your question text here]");
                    // Add code to accept user answers and validate them
                    input.nextLine(); // Simulates user entering an answer
                }
                System.out.println("--- Quiz Completed! ---");
            } else if (!userChoice.equalsIgnoreCase("N")) {
                System.out.println("Invalid input! Please enter Y or N.");
            }

            // After quiz completion, ask if user wants to retake
            if (userChoice.equalsIgnoreCase("Y")) {
                System.out.println("\nDo you want to take the quiz again? (Y/N)");
                userChoice = input.nextLine().trim();
            }

        } while (userChoice.equalsIgnoreCase("Y")); // Loop restarts if user chooses to retake

        // Exit flow when user selects "N"
        System.out.println("We hope to see you again soon!");
        input.close(); // Clean up scanner resource
        System.exit(0);
    }
}

Key improvements and breakdown:

  • Loop-driven menu flow: The outer do-while loop ensures the program returns to the start prompt after each quiz completion, as long as the user wants to retake.
  • Fixed syntax issues: Corrected the misplaced else and incomplete do-while structure from your original code.
  • Scalable question handling: The sample for loop represents your full question set—swap this out with your actual logic (loading custom question sets, checking answers, scoring, etc.).
  • Input validation: Added a check for invalid inputs to guide the user instead of breaking the program.
  • Resource cleanup: Closed the Scanner to avoid unnecessary resource leaks.

When you run this code:

  1. The user is asked to start the quiz.
  2. If they choose "Y", all questions are completed in sequence.
  3. After finishing, they’re prompted to retake (return to quiz) or exit.
  4. Choosing "N" at any point triggers the goodbye message and exits the program.

内容的提问来源于stack exchange,提问作者Patricia Angelli Valderrama

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 12:17:40