Outside of your for-loop, you define two variables:
boolean isHeads = false;
boolean isTails = false;
Inside the loop, if you ever flip heads, you make isHeads
true, and if you ever flip tails, you make isTails
true. Unfortunately, you never reset these, so they become 'stuck' in the true
position.
Because they're stuck, you later hit two conditions in your list of conditions, not just one. You add then subtract the bet, so the total doesn't wind up changing.
And one small thing:
if(isHeads == true && userChoice == 2)
{
System.out.println("You lose");
totalMoney = totalMoney + userBet;
}
Right there, you probably wanted to subtract the bet, like you do further down.
It's not the cleanest way to fix your code, but it's the least typing: as the last instruction in the loop, you should clear out isHeads
and isTails
by setting both to be false
. A cleaner way will require some refactoring: just use one variable that's true for heads and false for tails, so you avoid this risk. Then every time you flip, you're resetting it anyway.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…