Do While 循环代码

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

我对这段代码中发生的事情感到很困惑。

我正在执行一个 do 循环,并要求仅在满足两个条件时才循环。但是当我将 bet 设置为 0 时,循环继续。

我是不是漏掉了什么?

好吧,如果 bet 是 0,你永远不会分配 pot = game1.returnPotAmount();,所以 pot 可能仍然是 0(如果这是它的初始值),这意味着循环不会终止。

也许您希望您的条件是 - while (pot != 0 && bet != 0);

这是您的循环终止代码:

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

这表示(实际上)"keep going while the pot is not zero and the bet is zero"。但是你想在赌注为零时终止(而不是继续)。所以你的代码应该测试下注不为零......在 "keep going".

关键字 while 与英文单词 "while" 的意思大致相同。