Problem 2 High Low Game (40 points) Create a game where you start with $100 and
ID: 3544655 • Letter: P
Question
Problem 2 High Low Game (40 points)
Create a game where you start with $100 and spin a 10- digit spinner which lands on the value 1-10 (inclusive.) Guess whether the next spin will be higher or lower. If you guess correctly, you win $10. If incorrect, you lose $10, and if you tie, your money stays intact.
Continue to prompt for another spin and high/low guess until the user chooses to Quit the game. Your program output should match the sample run shown below:
SAMPLE RUN:
Note: This lab requires random numbers and also String comparisons.
> java HighLow
SPINNER HIGH-LOW GAME
The spinner has 20 numbers from 1-20
You have $ 100
Press Enter to take the first spin...
You spun 3
Spin again... will it be (H)igher or (L)ower or (Q)uit?
You spun 19
You won $10! You have $ 110
Spin again... will it be (H)igher or (L)ower or (Q)uit?
You spun 18
You lost $10! You have $ 100
Spin again... will it be (H)igher or (L)ower or (Q)uit?
You spun 10
You won $10! You have $ 110
Spin again... will it be (H)igher or (L)ower or (Q)uit?
You spun 20
You won $10! You have $ 120
Spin again... will it be (H)igher or (L)ower or (Q)uit?
You spun 15
You won $10! You have $ 130
Spin again... will it be (H)igher or (L)ower or (Q)uit?
You spun 11
You won $10! You have $ 140
Spin again... will it be (H)igher or (L)ower or (Q)uit?
Ended game with $ 140
Explanation / Answer
please rate - thanks
you say numbers 1-10 1 place, 1-20 elsewhere, if it's 20, change value of max to 20
any questions - ask
import java.util.*;
public class main
{public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Random r=new Random();
int bank=100,n,prev,max=10;
char guess;
System.out.println("SPINNER HIGH-LOW GAME");
System.out.println("The spinner has "+max+" numbers from 1-"+max);
System.out.println("You have $"+bank);
System.out.print("Press Enter to take the first spin...");
in.nextLine();
n=r.nextInt(max)+1;
System.out.println("You spun "+n);
System.out.print("Spin again... will it be (H)igher or (L)ower or (Q)uit? ");
guess=in.nextLine().charAt(0);
while(Character.toUpperCase(guess)!='Q')
{prev=n;
n=r.nextInt(max)+1;
System.out.println("You spun "+n);
if(prev>n)
if(Character.toUpperCase(guess)=='H')
{System.out.print("You lost $10 ");
bank-=10;
}
else
{System.out.print("You won $10 ");
bank+=10;
}
else
if(Character.toUpperCase(guess)=='L')
{System.out.print("You lost $10 ");
bank-=10;
}
else
{System.out.print("You won $10 ");
bank+=10;
}
System.out.println("You have $"+bank);
System.out.print("Spin again... will it be (H)igher or (L)ower or (Q)uit? ");
guess=in.nextLine().charAt(0);
}
System.out.println("Ended game with $"+bank);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.