How to Write Java Programs for a Slot Machine

by Paul Gun

Java programming was released in 1995 by Sun Microsystems and is related to the C and C++ programming languages. Java can be run on any machine that can carry the Java Virtual Machine, which interprets Java bytecode. One of the simpler programs to run as an exercise in Java is the slot machine. This program simulates the process of running an actual slot machine by generating three random numbers every time the user plays, determining if two or three of the numbers match, and letting the user know the result.


Import the utilities package of java and declare your class and "main" method. In this case you can declare these to be public. import java.util.*; public class Slotmachine { public static void main(String[] args) { Initialize a scanner and a random number generator. Be sure to declare the integers you will use before starting your 'while' loop. Scanner scans = new Scanner ( Random number = new Random(); int decision, firstnum, secondnum, thirdnum;


Within the first "while" loop, establish the integers you declared using the random number generator. while(decision == 1 && decision != -1){ firstnum = number.nextInt(10); secondnum = number.nextInt(10); thirdnum = number.nextInt(10); Ask the user if he wants to play slots, giving him instructions on how to indicate a yes-or-no response. Assign the user's answer to a variable name and use this as the condition in both of your "while" loops. System.out.print("Do you wish to start playing the slot machine? (Enter 1 for yes and -1 for no):"); decision = scans.nextInt();


Create cases for each possibility of matching (or mismatching) numbers, and print the corresponding response in a second 'while' loop that lies within the first. Continue to use the condition that the user wishes to continue playing for this inner 'while' loop. Use an 'if' statement to determine if all the numbers match, and use an 'else if' statement if any two of the numbers match. Use an 'else' statement if none of the numbers match. For example, if the first and third numbers match, print "Two of your numbers match!" while(decision == 1 && decision != -1) { System.out.println("|"+firstnum+"|"+secondnum+"... if(firstnum == secondnum && secondnum == thirdnum) {System.out.println("All of your numbers match!"); } else if (firstnum == secondnum && secondnum != thirdnum) {System.out.println("Two of your numbers match!"); } Continue this method for each case.


Ask if the user would like to continue playing or stop. Do this within the inner "while" loop. Assign the input to the previously chosen variable that was used before the outer "while" loop began. System.out.print("Would you like to keep playing?(Enter 1 for yes and -1 for no):"); decision = scans.nextInt();

Items you will need

About the Author

Paul Gun graduated from the California Institute of Technology with a Bachelor of Science in bioengineering and English. His laboratory and research experience has covered molecular biology, chemistry, computer programming and other scientific areas.

More Articles

Photo Credits

  • photo_camera Images