// Here I have some code that takes in scanner and random parameters to build a
ID: 641600 • Letter: #
Question
// Here I have some code that takes in scanner and random parameters to build a simplistic dice game. The output should be as follows,
/* OUTPUT OF PROGRAM:
How much would you like to add to your bankroll? 57.07
What is the value of your next bet ($0.00 - 57.07)? 7.07
What is your guess of the dice roll?
3
The rolls are: 2 2 6 // If my dice matches me guess once, odds are 1:1, If my dice matches me guess twice, odds are 2:1, and if all 3 match the guess then odds are 10:1
Your winnings are: $0.00 // corresponds to no match of the rolls with the guess, you lose what you bet!!!!
After bet 1 your bankroll is: 50.0
Do you want to play again? (y/n) n // boolean input
What is the value of your next bet ($0.00 - 50.0)? */ //Note here that when I tell the program a false string, it still outputs as if I told it I want to play again.
// What am I doing wrong here? I need the program to end if I tell it anything other than "y". I also need a way to round any infinite decimal places for my final bankroll, etc.
// HERE IS WHAT I WROTE:
import java.util.Scanner;
import java.util.Random;
public class ChuckALuck {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
Random r = new Random(System.currentTimeMillis());
Scanner s = new Scanner(System.in);
Scanner q = new Scanner(System.in);
// BankRoll(scan);
play(scan,r);
}
public static double BankRoll(Scanner scan) {
System.out.print("How much would you like to add to your bankroll? ");
double n = scan.nextDouble();
return n;
}
public static void play(Scanner scan, Random r) {
double n = BankRoll(scan);
Boolean keepPlaying = true;
while (keepPlaying == true) {
for ( int i = 1; i<=i;i++) {
System.out.print("What is the value of your next bet ($0.00 - "+n+")? ");
double bet = scan.nextDouble();
double winnings = bet;
int roll1 = diceroll(r);
int roll2 = diceroll(r);
int roll3 = diceroll(r);
System.out.println("What is your guess of the dice roll?");
int a = scan.nextInt();
if (a == roll1 || a == roll2 || a == roll3) {
n = n + bet;
System.out.println("Your winnings are: "+winnings);
System.out.println("After bet " + i + " your bankroll is: "+ n );
}
if (a == roll1 && a == roll2) {
n = n + 2*bet;
winnings = 2*bet;
System.out.println("Your winnings are: "+winnings);
System.out.println("After bet " + i + " your bankroll is: "+ n );
}
if (a == roll1 && a == roll3) {
n = n + 2*bet;
winnings = 2*bet;
System.out.println("Your winnings are: "+winnings);
System.out.println("After bet " + i + " your bankroll is: "+ n );
}
if (a == roll2 && a == roll3) {
n = n + 2*bet;
winnings = 2*bet;
System.out.println("Your winnings are: "+winnings);
System.out.println("After bet " + i + " your bankroll is: "+ n );
}
if (a == roll1 && a == roll2 && a == roll3) {
n = n + 10*bet;
winnings = 10*bet;
System.out.println("Your winnings are: "+winnings);
System.out.println("After bet " + i + " your bankroll is: "+ n );
}
if ((a>roll1 || aroll2 || aroll3 || a
n = n - bet;
System.out.println("Your winnings are: $0.00");
System.out.println("After bet " + i + " your bankroll is: "+ n );
}
System.out.print("Do you want to play again? (y/n) ");
String response = scan.next();
if ( response.equals("y")) {
keepPlaying = true;
}
else if(!(response.equalsIgnoreCase("y"))) {
keepPlaying = false;
}
}
}
}
public static int diceroll(Random numGen) {
int num = numGen.nextInt(6) + 1;
return num;
}
}
Explanation / Answer
public static void play(Scanner scan, Random r) {
double n = BankRoll(scan);
Boolean keepPlaying = true;
int i = 0;
//use decimal format to easily remove trailing zeros you can also use Math.ceil to round down
DecimalFormat df = new DecimalFormat("####.##");
/*
you had an infinite loop going with the for loop as it never returned control back to the while loop
while(keepPlaying){
for(int i = 0; i <= i; ++i){
if ( response.equals("y")) {
keepPlaying = true;
}else if(!(response.equalsIgnoreCase("y"))) {
keepPlaying = false;
this assignment will never be evaluated due to i<=i always being true in for loop
}
}
}
if you wish to keep the for loop style use
for(int i = 0; ; ++i)
replace the while loop
and use this to kill the loop
if (!response.equals("y"))
break;
*/
while (keepPlaying) {
System.out.print("What is the value of your next bet ($0.00 - "+ n + ")? ");
double bet = scan.nextDouble();
double winnings = bet;
int roll1 = diceroll(r);
int roll2 = diceroll(r);
int roll3 = diceroll(r);
System.out.println("What is your guess of the dice roll?");
int a = scan.nextInt();
System.out.println("Dice1 = "+roll1+" Dice2= "+roll2+" Dice3= "+roll3+" Your guess is "+a);
//base case check if your guess matches any of the dice
if (a == roll1 || a == roll2 || a == roll3) {
//next case check to see if we meet the 2:1 or 10:1 payout
if ((a == roll1 && (a == roll2 || a == roll3)) || (a == roll2 && (a == roll1 || a == roll3))) {
//we know that a equals roll1 or roll2 so check for 10:1 payout
if((a == roll2 && a == roll3) || (a == roll1 && a == roll3)){
//congratulations you match all three
//n = Math.ceil(n + 10 * bet);
//winnings = Math.ceil(10 * bet);
n = Double.valueOf(df.format(n+10*bet));
winnings = Double.valueOf(df.format(10*bet));
System.out.println("Your winnings are: " + winnings);
System.out.println("After bet " + i + " your bankroll is: "+ n);
}else{
//congratulations you match at least 2
//n = Math.ceil(n + 2 * bet);
//winnings = Math.ceil(2 * bet);
n = Double.valueOf(df.format(n+2*bet));
winnings = Double.valueOf(df.format(2*bet));
System.out.println("Your winnings are: " + winnings);
System.out.println("After bet " + i + " your bankroll is: "+ n);
}
}else{
//we matched only one
//n = Math.ceil(n + bet);
n = Double.valueOf(df.format(n+bet));
winnings = Double.valueOf(df.format(bet));
System.out.println("Your winnings are: " + winnings);
System.out.println("After bet " + i + " your bankroll is: "+ n);
}
} else {
//we matched none
n = n - bet;
winnings = 0;
System.out.println("Your winnings are: " + winnings);
System.out.println("After bet " + i + " your bankroll is: "+ n);
}
System.out.print("Do you want to play again? (y/n) ");
//to ensure that Y and y kill the loop use toLowerCase() of the return String from scan.next()
String response = scan.next().toLowerCase();
if (!response.equals("y")) {
//this break will kill the while loop
//keepPlaying will always be true thus infinite loop till any user input not Y or y
break;
}else{
++i;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.