Advertisement
Guest User

LuckySevens.java

a guest
Dec 1st, 2015
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 KB | None | 0 0
  1.  
  2.    /*LuckySevens.java
  3.    Simulate the game of lucky sevens until all funds are depleted.
  4.    1) Rules:
  5.            roll two dice
  6.            if the sum equals 7, win $4, else lose $1
  7.    2) The inputs are:
  8.            the amount of money the user is prepared to lose
  9.    3) Computations:
  10.            use the random number generator to simulate rolling the dice
  11.            loop until the funds are depleted
  12.            count the number of rolls
  13.            keep track of the maximum amount
  14.    4) The outputs are:
  15.            the number of rolls it takes to deplete the funds
  16.            the maximum amount
  17.    */
  18. import java.util.Scanner;
  19. import java.util.Random;
  20. public class LuckySevens
  21. {    
  22.   public static void main (String [] args)
  23.   {
  24.     Scanner reader = new Scanner(System.in);      
  25.     Random generator = new Random();
  26.            
  27.     int
  28.       die1,             // die one
  29.       die2,             // die two          
  30.       dollars,          // initial number of dollars (input)          
  31.       count,            // number of rolls to reach depletion          
  32.       maxDollars,       // maximum amount held by the gambler          
  33.       countAtMax;       // count when the maximum is achieved
  34.      
  35.       // Request the input      
  36.       System.out.print("How many dollars do you have? ");      
  37.       dollars = reader.nextInt();
  38.      
  39.       // Initialize variables      
  40.       maxDollars = dollars;      
  41.       countAtMax = 0;      
  42.       count = 0;
  43.      
  44.       // Loop until the money is gone      
  45.       while (dollars > 0)
  46.       {          
  47.         count++;
  48.        
  49.         // Roll the dice.          
  50.         die1 = generator.nextInt (6) + 1; // 1-6          
  51.         die2 = generator.nextInt (6) + 1; // 1-6
  52.          
  53.         // Calculate the winnings or losses          
  54.         if (die1 + die2 == 7)            
  55.           dollars += 4;
  56.         else
  57.           dollars -= 1;
  58.          
  59.         // If this is a new maximum, remember it          
  60.         if (dollars > maxDollars)
  61.         {            
  62.           maxDollars = dollars;            
  63.           countAtMax = count;
  64.         }
  65.       }
  66.      
  67.       // Display the results
  68.       System.out.println("You are broke after " + count + " rolls.\n" +          
  69.                         "You should have quit after " + countAtMax +          
  70.                         " rolls when you had $" + maxDollars + ".");    
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement