Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

if statement - How do I update and print an int whenever for-loop is ran

I made a program where the user is given 10 rounds to make as much money as he/she can using bets. The user gives a bet, then picks heads or tails. The loop works sometimes, but then other times it won't add nor subtract the bet from the total money. Is the for-loop keeping track of all the wins and losses or something?

Here's the code:

package wallonsProject;

import java.util.Scanner;

public class WallonProject {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        Scanner userInput = new Scanner(System.in);
        
        int totalMoney = 100000;
        
        //Coin faces
        boolean isHeads = false;
        boolean isTails = false;
        
        //CoinToss result
        int coinTossResult;
        
        //number of rounds
        int numOfRounds = 10;

            for(int i = 0; i < numOfRounds; i++)
            {

                //Betting and user Choice
                System.out.println("How much would you like to bet?");
                int userBet = userInput.nextInt();
                
                //User bet must be less than the total money they have or else game exits
                if(userBet > totalMoney)
                {
                    System.exit(0);
                }
                
                System.out.println("Press 1 for heads, or 2 for tails");
                int userChoice = userInput.nextInt();
                
                System.out.println("Your starting money is " + totalMoney);

                //coinToss
                double coinToss = Math.random();
                {
                    if(coinToss >= 0.5)
                    {
                        isHeads = true;
                        System.out.println("Toss is heads");
                        coinTossResult = 1;
                        System.out.println("The toss is " + coinTossResult);
                    }
                    if(coinToss < 0.5)
                    {
                        isTails = true;
                        System.out.println("Toss is tails");
                        coinTossResult = 2;
                        System.out.println("The toss is " + coinTossResult);
                    }
                    
                    System.out.println("You chose " + userChoice);
                    
                    //All different outcomes of WINNING results
                    if(isHeads == true && userChoice == 1)//if your choice is heads and cointossresult is heads
                    {
                        System.out.println("You win");
                        totalMoney = totalMoney + userBet;
                    }
                    if(isTails == true && userChoice == 2)//if your choice is tails and cointossresult is tails
                    {
                        System.out.println("You win");
                        totalMoney = totalMoney + userBet;
                    }
                    
                    //All different outcomes of LOSING results
                    if(isHeads == true && userChoice == 2)
                    {
                        System.out.println("You lose");
                        totalMoney = totalMoney + userBet;
                    }
                    if(isTails == true && userChoice == 1)
                    {
                        System.out.println("You lose");
                        totalMoney = totalMoney - userBet;
                    }
                    
                    
                    System.out.println("You bet " + userBet);
                    System.out.println("Your total money is " + totalMoney);
            }

        }

    }        

}
question from:https://stackoverflow.com/questions/65851325/how-do-i-update-and-print-an-int-whenever-for-loop-is-ran

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...