Thing

  • October 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Thing as PDF for free.

More details

  • Words: 234
  • Pages: 2
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)); }

}

Related Documents

Thing
October 2019 29
Thing
December 2019 25
Thing
December 2019 25
Big Thing
November 2019 20
Ski Thing
November 2019 16
Wild Thing
June 2020 6