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