Home » Java » Slot machine infinite loop Trouble in JAVA

Slot machine infinite loop Trouble in JAVA

Posted by: admin July 23, 2018 Leave a comment

Questions:

I am writing a basic slot machine code and seem to have it stuck in an infinite loop when the program is ran. If any one could shed some light on where about this error is, or something that you would recommend. All the help is appreciated.

public class Java_Lab_3 {
    public static void main(String[] args) {
        int endgame;  
        int slot1;
        int slot2;
        int slot3;

        Random rand =  new Random();
        Scanner keyboard = new Scanner(System.in);  

        System.out.println("Want to test your luck?");
        System.out.println("To Spin, type any positive number");
        System.out.println("To End Game, type -1 and press enter ");

        endgame = keyboard.nextInt();

        while (endgame != -1) {
            slot1 = rand.nextInt(10);
            slot2 = rand.nextInt(10);
            slot3 = rand.nextInt(10);

            System.out.println(slot1 + slot2 + slot3);
            System.out.println(endgame);

            if (slot1 == slot2 && slot1 == slot3) { // Then they have JACKPOT
                System.out.println("You've Won!");
            } else if (slot1 == slot2 || slot2 == slot3 || slot1 == slot3) { // They MATCHED 2
               System.out.println("We have twins... You've matched a pair!");
            } else {
                System.out.println("Sucks to Suck, don't quit your day job!");
            }
        }
    }
}
Answers: