Write a program that acts as a simple \"slot machine\". The user starts with 100
ID: 3612142 • Letter: W
Question
Write a program that acts as a simple "slot machine". The user starts with 100 tokens and with each pullof the handles loses one token. The computer spins threewheels each consisting of the numbers 1,2, and 3. If allthree of the numbers are 1 the user gets 4 tokens; if all three are2 the user gets 8 tokens; if all three are 3 the user gets 12tokens. Program output should look similar to
You have 100 tokens Pull? Y
[1] [3] [2]
You lost
You have 99 tokens Pull? Y
[2] [2] [2]
you won 8 tokens!
You have 106 tokens Pull? n
Thanks for playing
Must use JOptionPane! Error messages! programmust stop if the user has no token left!
you can use the random number generator in the book -remember that
x= random(); gives you a random number between 0 andone. To get a random number which is an integer 1,2,3 youwould use:
x = (( int) (random()*10 +.5)) +1;
you can also use a function in the utility class called nextInt(); To use this you need to import the utilityclass.
import java.util.*
Then in the main you need to creatan object of Random:
Random number = new Random()
then you can use
num= number.nextInt(3) +1;
Explanation / Answer
please rate - thanks import java.util.*; import javax.swing.*; import java.lang.*; public class slotmachine2 { public static void main(String []args) { Random number = new Random(); int [] pull=new int[3]; String response,mess; char c; int tokens,i,plustokens=0; boolean won; do { tokens=100; response = JOptionPane.showInputDialog(null, "You have "+tokens+"tokens Pull? "); c = response.charAt(0); while((c=='Y'||c=='y')&&(tokens>0)) {won=false; tokens--; for(i=0;iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.