简体   繁体   中英

Do While Loop Code

        do{
        game1.getBetAmountFromUser();
        bet = game1.returnBetAmount();

        if (bet!=0){
            game1.playGame();
            pot = game1.returnPotAmount();
            System.out.println("");             
        }
        else{
            System.out.println("You end the game with pot of $" + formatter.format(pot));
            bet = game1.returnBetAmount();
            }
        }while (pot != 0 && bet == 0);

I am very confused with what is happening in this code.

I am doing a do loop and asking to only loop when two criteria are met. But when I set bet to 0, the loop continues.

Am I missing something?

Well, if bet is 0, you never assign pot = game1.returnPotAmount(); , so pot may remain 0 (if that is its initial value), which means the loop won't terminate.

Perhaps you intended your condition to be - while (pot != 0 && bet != 0);

Here is your loop termination code:

  ... } while (pot != 0 && bet == 0);

This says (in effect) "keep going while the pot is not zero and the bet is zero". But you want to terminate (NOT keep going) when the bet is zero. So your code should test for the bet being NOT zero ... in the "keep going".

The keyword while means roughly the same thing as the word "while" in English.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM