For this program you will write a Java program that plays the dice game High-Low
ID: 3529588 • Letter: F
Question
For this program you will write a Java program that plays the dice game High-Low. In this game a player places a bet on whether the sum of two dice will come up High (totalling 8 or higher), Low (totalling 6 or less) or Sevens (totalling exactly 7). If the player wins, he receives a payout based on the schedule given in the table below:
The player will start with $100 for wagering. If the player chooses to wager $0 or if he runs out of money, the program should end. Otherwise it should ask him whether he's wagering on High, Low or Sevens, display the results of the die rolls, and update his money total accordingly.
This is a sample transcript of what your program should do. Items inboldare user input and should not be put on the screen by your program.
You have 100 dollars.
Enter an amount to bet (0 to quit):50
High, low or sevens (H/L/S):H
Die 1 rolls: 1
Die 2 rolls: 5
Total of two dice is: 6
You lost!
You have 50 dollars.
Enter an amount to bet (0 to quit):25
High, low or sevens (H/L/S):L
Die 1 rolls: 6
Die 2 rolls: 2
Total of two dice is: 8
You lost!
You have 25 dollars.
Enter an amount to bet (0 to quit):12
High, low or sevens (H/L/S):H
Die 1 rolls: 2
Die 2 rolls: 6
Total of two dice is: 8
You won 12 dollars!
You have 37 dollars.
Enter an amount to bet (0 to quit): 18
High, low or sevens (H/L/S):S
Die 1 rolls: 5
Die 2 rolls: 4
Total of two dice is: 9
You lost!
You have 19 dollars.
Enter an amount to bet (0 to quit):9
High, low or sevens (H/L/S):H
Die 1 rolls: 3
Die 2 rolls: 3
Total of two dice is: 6
You lost!
You have 10 dollars.
Enter an amount to bet (0 to quit):5
High, low or sevens (H/L/S):L
Die 1 rolls: 2
Die 2 rolls: 4
Total of two dice is: 6
You won 5 dollars!
You have 15 dollars.
Enter an amount to bet (0 to quit):7
High, low or sevens (H/L/S):H
Die 1 rolls: 5
Die 2 rolls: 6
Total of two dice is: 11
You won 7 dollars!
You have 22 dollars.
Enter an amount to bet (0 to quit):0
You have 22 dollars left
Goodbye!
Note that your output depends on the choices made by the user. Remember to check that the user's inputs are valid:
You have 100 dollars.
Enter an amount to bet (0 to quit): -100
Your bet MUST be between 0 and 100 dollars
You have 100 dollars.
Enter an amount to bet (0 to quit): 1000
Your bet MUST be between 0 and 100 dollars
You have 100 dollars.
Enter an amount to bet (0 to quit): 100
High, low or sevens (H/L/S): h
Die 1 rolls: 2
Die 2 rolls: 6
Total of two dice is: 8
You won 100 dollars!
You have 200 dollars.
Enter an amount to bet (0 to quit): 200
High, low or sevens (H/L/S): s
Die 1 rolls: 5
Die 2 rolls: 3
Total of two dice is: 8
You lost!
You have 0 dollars left
Goodbye!
Skeleton:
Payout
High
1 x Wager
Low
1 x Wager
Sevens
4 x Wager
Explanation / Answer
import java.util.Random;
import java.util.Scanner;
public class DiceGame {
public static void main(String args[]) throws Exception {
int curAmount = 100; //dollars
Scanner recorder = new Scanner(System.in);
Random randomGenerator = new Random();
while(curAmount == 0) {
System.out.println("You have "+curAmount+"dollars");
System.out.println("Enter an amount to bet (0 to quit): ");
int betAmount = recorder.nextInt();
if(betAmount == 0) {
System.out.println("Good bye!");
System.exit(0);
}
System.out.println("Enter High, low or sevens (H/L/S)");
char option = (char) recorder.nextByte();
int dice1 = randomGenerator.nextInt(6)+1;
System.out.println("Die 1 rolls: "+dice1);
int dice2 = randomGenerator.nextInt(6)+1;
System.out.println("Die 2 rolls: "+dice2);
System.out.println("Total of two dice is:: "+(dice1+dice2));
int total = (dice1+dice2);
if((option == 'H' || option == 'h')) {
if(total >= 8) {
System.out.println("You won "+betAmount+" dollars");
curAmount += betAmount;
} else {
System.out.println("You lost!");
curAmount -= betAmount;
}
} else if((option == 'L' || option == 'l')) {
if(total <= 6) {
System.out.println("You won "+betAmount+" dollars");
curAmount += betAmount;
} else {
System.out.println("You lost!");
curAmount -= betAmount;
}
} else if((option == 'S' || option == 's')) {
if(total == 7) {
System.out.println("You won "+ (4 * betAmount) +" dollars");
curAmount += 4 * betAmount;
} else {
System.out.println("You lost!");
curAmount -= betAmount;
}
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.