簡體   English   中英

Java 新手試圖弄清楚如果給出某個輸入,如何在 do-while 循環中跳回

[英]Newbie to Java trying to figure out how to jump BACK in a do-while loop if a certain input is given

這是我目前擁有的代碼,我試圖弄清楚如何從案例“A”跳回到 do-while 循環的頂部,以允許用戶選擇另一個操作。 這個想法是,用戶將能夠檢查他們的余額,然后在檢查他們的余額后返回並充值他們的卡或購買洗滌。 我只知道如何從這一點結束循環,而不知道如何向后跳到上一個輸入。

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();

    switch (actionSelection) { //I haven't defined all the cases yet! //The other cases should not need to refer to other classes, all the actions are within the WashCard
    case "A":
        System.out.println("Your current balance is: " + this.cardBalance + ".00 DKK.");
        sentinel = true;
        System.out.println("Would you like to do something else? Press [B] to go back or [Q] to quit.");

        break;

    case "B":
        System.out.println("How much would you like to deposit?\nPlease type in a number between 200 and 1000.");
        //user input
        break;

    case "C":
        //any selection should display card balance
        //insufficient funds conditional statement:
        if (this.cardBalance < 50) {
            sentinel = true;
            System.out.println("Not enough credit! Please top-up your card first");
            System.out.println("Your current balance is: " + this.cardBalance + ".00 DKK.");

        }

        //only 50 DKK left on the card:
        else if (this.cardBalance == 50) {
            sentinel = false;
            System.out.println("You only have enough credit for the ECONOMY WASH.");
            System.out.println("Your current balance is: " + this.cardBalance + ".00 DKK.");
        }

        //any other choice:
        else {
            sentinel = false;
            System.out.println("Your current balance is: " + this.cardBalance + ".00 DKK.");
        }

        break;

    default:
        sentinel = true;
        System.out.println("Please enter a valid input!");

    }

} while ( sentinel == true );

你不需要這個。 這個解決方案打破了 OOP 原則。 要解決它,您應該將不同的邏輯封裝在不同的方法中。

public class Foo {

    public static void main(String... args) {
        Scanner scan = new Scanner(System.in);
        proceed(scan);
    }

    private static final char CHECK_FUNDS = 'A';
    private static final char TOP_UP_CARD = 'B';
    private static final char BUY_WASH = 'C';
    private static final char QUIT = 'Q';

    public static void proceed(Scanner scan) {
        while (true) {
            System.out.println("Please select an option:");
            System.out.format("[%s] - CHECK FUNDS\n", CHECK_FUNDS);
            System.out.format("[%s] - TOP-UP CARD\n", TOP_UP_CARD);
            System.out.format("[%s] - BUY WASH\n", BUY_WASH);
            System.out.format("[%s] - quit\n", QUIT);
            System.out.print("> ");

            String menu = scan.nextLine().toUpperCase();
            char ch = menu.length() == 1 ? menu.charAt(0) : '\0';

            if (ch == CHECK_FUNDS)
                onCheckFunds();
            else if (ch == TOP_UP_CARD)
                onTopUpCard();
            else if (ch == BUY_WASH)
                onBuyWash();
            else if (ch == QUIT)
                return;

            System.err.println("incorrect input");
        }
    }

    private static void onCheckFunds() {
        System.out.println("onCheckFunds");
    }

    private static void onTopUpCard() {
        System.out.println("onCheckFunds");
    }

    private static void onBuyWash() {
        System.out.println("onBuyWash");
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM