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

Java新手求助:如何从do-while循环的case"A"分支跳回循环顶部

Fixing the Loop Jump Issue in Your Java Wash Card Program

Hey there! Let's get your program to let users bounce back to the main menu after checking their balance. The main issue right now is that in your case "A" branch, you're asking users if they want to do something else—but you aren't actually handling their response, and your sentinel logic needs a tiny tweak to keep the loop running when needed.

Here's how to adjust your code step by step:

1. Fix the case "A" Logic

Right now, you set sentinel = true and print a prompt, but don't wait for user input. We need to add a small inner loop to handle their choice (go back to menu or quit):

case "A":
    System.out.println("Your current balance is: " + this.cardBalance + ".00 DKK.");
    boolean validContinueChoice = false;
    // Inner loop to force valid input from the user
    while (!validContinueChoice) {
        System.out.println("Would you like to do something else? Press [B] to go back to main menu or [Q] to quit.");
        String continueSelection = scan.nextLine().toUpperCase(); // Handle lowercase inputs too
        
        switch (continueSelection) {
            case "B":
                sentinel = true; // Keep the main loop running to show the menu again
                validContinueChoice = true;
                break;
            case "Q":
                sentinel = false; // Exit the main loop
                validContinueChoice = true;
                break;
            default:
                System.out.println("Oops, that's not a valid option! Please enter B or Q.");
        }
    }
    break;

2. Adjust Other Cases to Keep the Loop Running

Looking at your case "C" branch, you set sentinel = false in some scenarios—which would exit the loop after a wash. If you want users to keep using the program after a wash, change those to sentinel = true instead. For example:

case "C":
    System.out.println("Your current balance is: " + this.cardBalance + ".00 DKK.");
    if (this.cardBalance < 50) {
        System.out.println("Not enough credit! Please top-up your card first");
        sentinel = true; // Send user back to main menu
    } else if (this.cardBalance == 50) {
        System.out.println("You only have enough credit for the ECONOMY WASH.");
        this.cardBalance -= 50; // Deduct the wash cost
        System.out.println("Wash completed! Your new balance is: " + this.cardBalance + ".00 DKK.");
        sentinel = true; // Back to main menu
    } else {
        // Add wash type selection here if you want
        System.out.println("Select a wash type:\n[E] Economy (50 DKK), [S] Standard (75 DKK), [P] Premium (100 DKK)");
        String washType = scan.nextLine().toUpperCase();
        
        switch (washType) {
            case "E":
                this.cardBalance -= 50;
                break;
            case "S":
                this.cardBalance -= 75;
                break;
            case "P":
                this.cardBalance -= 100;
                break;
            default:
                System.out.println("Invalid wash type! No charge applied.");
                sentinel = true;
                break;
        }
        System.out.println("Wash completed! Your new balance is: " + this.cardBalance + ".00 DKK.");
        sentinel = true; // Back to main menu
    }
    break;

3. Set the Initial sentinel Value

Make sure you initialize sentinel to true before the loop starts, so the main menu loads the first time:

boolean sentinel = true;
do {
    // Your existing menu print and input code here
} while (sentinel == true);

How It Works Now

  • When a user checks their balance (case A), they’ll be prompted to choose between going back to the main menu (B) or quitting (Q).
  • After topping up (case B) or buying a wash (case C), the loop will automatically jump back to the main menu, letting users keep making selections until they choose Q.
  • The inner loop in case A ensures users can’t enter invalid options—they’ll be prompted again until they pick B or Q.

Here’s the full adjusted code for reference:

import java.util.Scanner;

public class WashCard {
    private double cardBalance;
    private Scanner scan = new Scanner(System.in);

    public WashCard(double initialBalance) {
        this.cardBalance = initialBalance;
    }

    public void runMenu() {
        boolean sentinel = true;
        do {
            System.out.println("Please select an option:\nPress [A] to CHECK FUNDS, press [B] to TOP-UP CARD, press [C] to BUY WASH or press [Q] to quit.");
            String actionSelection = scan.nextLine().toUpperCase(); // Handle lowercase inputs

            switch (actionSelection) {
                case "A":
                    System.out.println("Your current balance is: " + this.cardBalance + ".00 DKK.");
                    boolean validContinueChoice = false;
                    while (!validContinueChoice) {
                        System.out.println("Would you like to do something else? Press [B] to go back to main menu or [Q] to quit.");
                        String continueSelection = scan.nextLine().toUpperCase();
                        
                        switch (continueSelection) {
                            case "B":
                                sentinel = true;
                                validContinueChoice = true;
                                break;
                            case "Q":
                                sentinel = false;
                                validContinueChoice = true;
                                break;
                            default:
                                System.out.println("Oops, that's not a valid option! Please enter B or Q.");
                        }
                    }
                    break;
                case "B":
                    System.out.println("How much would you like to deposit?\nPlease type in a number between 200 and 1000.");
                    // Handle deposit input and update balance
                    if (scan.hasNextDouble()) {
                        double deposit = scan.nextDouble();
                        scan.nextLine(); // Consume the leftover newline
                        if (deposit >= 200 && deposit <= 1000) {
                            this.cardBalance += deposit;
                            System.out.println("Deposit successful! Your new balance is: " + this.cardBalance + ".00 DKK.");
                        } else {
                            System.out.println("Invalid amount! Please enter a number between 200 and 1000.");
                        }
                    } else {
                        System.out.println("Invalid input! Please enter a number.");
                        scan.nextLine(); // Clear invalid input
                    }
                    sentinel = true; // Back to main menu
                    break;
                case "C":
                    System.out.println("Your current balance is: " + this.cardBalance + ".00 DKK.");
                    if (this.cardBalance < 50) {
                        System.out.println("Not enough credit! Please top-up your card first");
                        sentinel = true;
                    } else if (this.cardBalance == 50) {
                        System.out.println("You only have enough credit for the ECONOMY WASH.");
                        this.cardBalance -= 50;
                        System.out.println("Wash completed! Your new balance is: " + this.cardBalance + ".00 DKK.");
                        sentinel = true;
                    } else {
                        System.out.println("Select a wash type:\n[E] Economy (50 DKK), [S] Standard (75 DKK), [P] Premium (100 DKK)");
                        String washType = scan.nextLine().toUpperCase();
                        
                        switch (washType) {
                            case "E":
                                this.cardBalance -= 50;
                                break;
                            case "S":
                                this.cardBalance -= 75;
                                break;
                            case "P":
                                this.cardBalance -= 100;
                                break;
                            default:
                                System.out.println("Invalid wash type! No charge applied.");
                                sentinel = true;
                                break;
                        }
                        System.out.println("Wash completed! Your new balance is: " + this.cardBalance + ".00 DKK.");
                        sentinel = true;
                    }
                    break;
                case "Q":
                    sentinel = false;
                    System.out.println("Thank you for using the wash card service!");
                    break;
                default:
                    System.out.println("Please enter a valid input!");
                    sentinel = true;
            }
        } while (sentinel == true);
    }

    public static void main(String[] args) {
        WashCard myCard = new WashCard(100); // Example initial balance
        myCard.runMenu();
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 16:17:59