package chapter6; /*LuckySevens.java Simulate the game of lucky sevens until all funds are depleted. 1) Rules: roll two dice if the sum equals 7, win $4, else lose $1 2) The inputs are: the amount of money the user is prepared to lose 3) Computations: use the random number generator to simulate rolling the dice loop until the funds are depleted count the number of rolls keep track of the maximum amount 4) The outputs are: the number of rolls it takes to deplete the funds the maximum amount */ import java.util.Scanner; import java.util.Random; public class LuckySevensModded { public static void main (String [] args) { Scanner reader = new Scanner(System.in); Random generator = new Random(); int die1, die2, dollars, count;
// two dice // initial number of dollars (input) // number of rolls to reach depletion
// Request the input System.out.print("How many dollars do you have? "); int dollarsetting = reader.nextInt(); dollars = dollarsetting; // Initialize variables count = 0; // Loop until the money is gone for (int q = 1; q <=100; q++){ dollars = dollarsetting; while (dollars > 0){ count++; // Roll the dice. die1 = generator.nextInt (6) + 1; // 1-6 die2 = generator.nextInt (6) + 1; // 1-6 // Calculate the winnings or loses if (die1 + die2 == 7) dollars += 4; else dollars -= 1; }
} System.out.println
("Average rolls = " + (count/100)); }
}