1

I'm having issues with a homework problem for my CS class. I'm supposed to make a slot machine and I can't get the continue to work, I get that the scanner nextLine method skips over the line and keeps what was there but it won't let me enter anything and it just ends the program. I also have to split this method up and make it 30 lines or less because of the formatting that is required by my professor. I also have to keep a running total for the winnings but I can't figure out how to do it for the jackpot.

/**
 * ProgrammingAssignment2.java
 * 
 * @author Corey Goff 
 * @version March 2013
 */
import java.util.Random;
import java.util.Scanner;
/**
* This class simulates a slot machine.
*/
public class SlotMachine
{
   /**
    * This is the main method.
    * 
    * @param args
    */
   public static void main(String[] args)
   {
       Scanner keyboard = new Scanner(System.in);
       Random random = new Random();
       String cont = "n";
       char answer;
       int coin = 0;
       int totalEntered = 0;
       int a;
       int b;
       int c;
       int n;
       int amountWon = 0;
       int dubs = coin * 2;
       int trips = coin * 4;

       while (cont.equals("n"))
       {
           a = random.nextInt(6);
           b = random.nextInt(6);
           c = random.nextInt(6);
           n = random.nextInt(991) +10;
           totalEntered += coin;
           System.out.println("How much would you like to bet? ");
           coin = keyboard.nextInt();

           switch (a) 
           {
               case 0:
                   System.out.println("Cherry");
                   break;
               case 1:
                   System.out.println("Orange");
                   break;
               case 2:
                   System.out.println("Plum");
                   break;
               case 3:
                   System.out.println("Bell");
                   break;
               case 4:
                   System.out.println("Melon");
                   break;
               default:
                   System.out.println("Bar");
           }

           switch (b) 
           {
               case 0:
                   System.out.println("Cherry");
                   break;
               case 1:
                   System.out.println("Orange");
                   break;
               case 2:
                   System.out.println("Plum");
                   break;
               case 3:
                   System.out.println("Bell");
                   break;
               case 4:
                   System.out.println("Melon");
                   break;
               default:
                   System.out.println("Bar");
           }

           switch (c) 
           {
               case 0:
                   System.out.println("Cherry");
                   break;
               case 1:
                   System.out.println("Orange");
                   break;
               case 2:
                   System.out.println("Plum");
                   break;
               case 3:
                   System.out.println("Bell");
                   break;
               case 4:
                   System.out.println("Melon");
                   break;
               default:
                   System.out.println("Bar");
           }

           if (a != b && a != c && b != c)
           {
               System.out.println("You have won $0");
           }
           else if (a == b || a == c || b == c)
           {
               System.out.println("Congratulations, you have won $" + dubs);
                  amountWon += dubs;
           }
           else if (a == b && a == c && a != 0)
           {
               System.out.println("Congratulations, you have won $" + trips);
                  amountWon += trips;
           }
           else if (a == 0 && b == 0 && c == 0)
           {
               System.out.println("Congratulations! You have won the jackpot of $" 
                   + (coin * n));

           }

           System.out.println("Continue? y/n ");
           cont = keyboard.nextLine();
       }
   }
}
Corey211
  • 59
  • 1
  • 1
  • 2

2 Answers2

2

After you call keyboard.nextInt(), you must call keyboard.nextLine() to dump the newline character in the buffer. nextInt() reads until it finds something that cannot be part of an int and leaves everything after the int sitting in the buffer.

See this post for more information: Reading Strings next() and nextLine() Java

Community
  • 1
  • 1
kronion
  • 711
  • 4
  • 14
0

To get your code to work (with little changes): Use keyboard.next(); instead of keyboard.nextLine();

change your initial cont = "n" to cont = "y", and have the while loop check if (cont.equals("y")).

This is a quick fix, there are many changes that could be made for efficiency and to keep up with standards, but I won't go into detail.

As for the under 30 lines requirement, I would start by using only one random generator to generate all 3 fruits in one structure. Ask yourself, why must you have separate random generators for each fruit? Why a new switch per fruit? One random vs 3 random generators, will produce the same randomness for your fruits.

dgund
  • 3,459
  • 4
  • 39
  • 64