如何退出ticketInput()循环并返回mainInputMenu()方法?
Hey there! Let's work through how to get your program to jump back to the main menu when the user selects option 11 in ticketInput(). First, let's look at the key issues in your current code, then fix them step by step.
Problem with Your Current Setup
Right now, once the user selects machine 1 in mainInputMenu(), the while (machineNumber == 1) loop runs infinitely—there's no way to break out of it to get back to the main menu selection. We need to add a way for ticketInput() to signal that the user wants to return to the main menu, then adjust the loops to respect that signal.
Step 1: Modify ticketInput() to Return a Status
Change the method to return a boolean where true means "return to main menu" and false means "stay in the current machine's menu":
public class Machine { // Reuse a single Scanner instead of creating a new one every time private static Scanner keyboard = new Scanner(System.in); // Your existing static variables (rSelected, aSum, cSum, creditSum) here public static boolean ticketInput() { String ticketCounter; int counter = 0; do { System.out.printf("Your choice: "); ticketCounter = keyboard.next(); if (ticketCounter.matches("[0-9]+")) { counter = Integer.parseInt(ticketCounter); } if (counter < 1 || counter > 11) { System.out.println("Try again!"); } } while (counter < 1 || counter > 11); switch (counter) { case 1: rSelected = "Uptown"; break; case 2: rSelected = "Downtown"; break; case 3: aSum = aSum + 1; break; case 4: if (aSum > 0) { aSum = aSum - 1; } else { System.out.println("Cannot go below 0."); } break; case 5: cSum = cSum + 1; break; case 6: if (cSum > 0) { cSum = cSum - 1; } else { System.out.println("Cannot go below 0."); } break; case 7: creditSum = creditSum + 0.25; break; case 8: creditSum = creditSum + 1.00; break; case 9: creditSum = creditSum + 2.00; break; case 10: System.out.println("-----------------\n" + "YOU GOT YOUR TICKET!!!\n" + "-----------------"); System.exit(0); case 11: // Signal we want to return to the main menu return true; default: break; } // For all other options, stay in the current machine return false; } // Your other Machine methods (writeOutput, readInput, etc.) here }
Step 2: Update machineOne() to Pass the Status
Modify machineOne() to return the status from ticketInput():
private static boolean machineOne(int machineNumber) { Machine.option = 1; Machine.writeOutput(); return Machine.ticketInput(); }
Step 3: Refactor mainInputMenu() to Handle the Status
Rewrite the main menu loop so it can exit the machine-specific loop when it gets the "return to main" signal:
public class Tickets { public static void main(String[] args) { mainInputMenu(); } private static void mainInputMenu() { // Main loop: keeps showing the machine selection menu until user exits while (true) { int machineNumber = Machine.readInput(); if (machineNumber == 1) { // Loop for machine 1 operations until user chooses to return to main while (true) { boolean returnToMain = machineOne(machineNumber); if (returnToMain) { break; // Exit machine 1 loop, go back to main menu } // If user didn't choose to return, re-show the machine 1 options Machine.writeOutput(); } } else if (machineNumber == 2) { // Add similar logic here for machine 2 if needed System.out.println("Machine 2 selected. Implement your logic here!"); } else if (machineNumber == 3) { System.exit(0); } else { System.out.println("Invalid machine number. Please try again."); } } } private static boolean machineOne(int machineNumber) { Machine.option = 1; Machine.writeOutput(); return Machine.ticketInput(); } }
Key Improvements
- Reusable Scanner: We now use one static Scanner in
Machineinstead of creating a new one each time (prevents resource leaks). - Clear Control Flow: The boolean return value makes it explicit when the user wants to return to the main menu, avoiding messy goto-style logic.
- Flexible Loops: The main menu runs in an infinite loop, so after returning from a machine, the user can select another machine or exit.
内容的提问来源于stack exchange,提问作者Twissted

