3

as the title suggests I am doing a program for homework that is a slot machine. I have searched around and I am pretty satisfied that the program works correctly enough for me. The problem Im having is on top of generating the random numbers, I am supposed to assign values for the numbers 1-5 (Cherries, Oranges, Plums, Bells, Melons, Bars). Then I am to display the output instead of the number when my program runs. Can anyone get me pointed in the right direction on how to do this please?

import java.util.Random;
import java.util.Scanner;



public class SlotMachineClass {


public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    int Coins = 1000;
    int Wager = 0;




    System.out.println("Steve's Slot Machine");
    System.out.println("You have " + Coins + " coins.");
    System.out.println("Enter your bet and press Enter to play");




    while (Coins > 0)
    {
    int first = new Random().nextInt(5)+1;
    int second = new Random().nextInt(5)+1;
    int third = new Random().nextInt(5)+1;

    Wager = input.nextInt();

    if(Wager > Coins)
             Wager = Coins;

    System.out.println(first + " " + second + " " + third);


    if(first == second && second == third)
    { Coins = Coins + (Wager * 3);
         System.out.println("You won " + (Wager * 3) + "!!!!" + " You now have " + Coins + " coins.");
         System.out.println("Enter another bet or close program to exit");}

    else if((first == second && first != third) || (first != second && first == third) || (first != second && second == third))
    { Coins = Coins + (Wager * 2);
         System.out.println("You won " + (Wager * 2) + "!!!" + " You now have " + Coins + " coins.");
         System.out.println("Enter another bet or close program to exit");}

    else {Coins = Coins - Wager;  
    System.out.println("You Lost!" + "\nPlay Again? if so Enter your bet.");}



    }

    while (Wager == 0)
    {
        System.out.println("You ran out of coins. Thanks for playing."); 
    }


}

}

3
  • Thanks for all the help. The first thing I thought of was an array. But the assignment and teacher wont allow them. I believe a switch statement is in order but I just never used one and dont really know how to code such a thing. Commented Mar 28, 2013 at 2:01
  • How are you supposed to assign the six possible fruits to the numbers 1-5? Is more than one fruit mapped to the same number? Commented Mar 28, 2013 at 2:21
  • sorry about that, its 0-5, thanks for noticing Commented Mar 28, 2013 at 2:27

5 Answers 5

4

If you have an int and want to have some String associated with that, there are a couple of ways to do that.

The first one is to have an array of Strings and look them up.

public static String[] text = new String[] {"Cherry", "Bell", "Lemon", "Bar", "Seven"};
public String getNameForReel(int reelValue) {
    return text[reelValue];
}
// And to call it...
System.out.println(getNameForReel(first)); //etc...

Or, you can do it in a switch statement (I don't prefer this, but you might):

public String getNameForReel(int reelValue) {
    switch(reelValue) {
       case 0: return "Cherry";
       case 1: return "Bell";
       case 2: return "Lemon";
       case 3: return "Bar";
       case 4: return "Seven";
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, I think that the switch statement will do the trick but being new to java im not sure what the code is "ForReel" or "reelValue" is that something I need in the program or is that substituting something, and if so, what exactly. Thanks :)
reelValue is an integer that you are passing to the method. In your case, it is either the variable first, second, or third. The getNameForReel is a method which take the reelValue you pass it and works on it.
reelValue would be whatever variable for which you're trying to get a string representation: println(getNameForReel(first) + " " and so forth.
Ok, just so I got this straight, I need to make 3 switch statements for the first, second and third, correct? The getNameForReel is my method that I may 'lend' to another class if need be? Sorry for being so slow on this stuff, we just did classes last week.
No, you only need one method. As others have suggested, you don't really need the method, you can just access the array directly! System.out.println(text[first]); For example.
2

You need a lookup table:

String[] text = new String[] {"Cherry", "Bell", "Lemon", "Bar", "Seven"};

Then you can just do

System.out.println(text[first] + " " + text[second] + " " + text[third]);

without creating more methods.

2 Comments

Mine uses no extra methods.
Much better (and +1). I'll clean up my comments. :-)
0

The non-array solution most likely to be used a by new programmer in an intro course would be a nested if-else:

String fruitToPrint = "";
if (num == 0)
   fruitToPrint = "Cherries";
else if (num == 1)
   fruitToPrint = "Oranges";
else if (num == 2)
   fruitToPrint = "Plums";
else if (num == 3)
   fruitToPrint = "Bells";
else if (num == 4)
   fruitToPrint = "Melons";
else if (num == 5)
   fruitToPrint = "Bars";
else
   System.out.println("Couldn't assign fruit from num=" + num);

System.out.println("The corresponding fruit was " + fruitToPrint); 

6 Comments

Ok, i see where you are coming from here, how exactly would incorporate that into my program so that all three int's get the name of the fruit?
@StevenEck Copy the above code block into your program 3 times, and in each copied block replace fruitToPrint with firstFruit, secondFruit, and thirdFruit. This will create 3 String instances and correctly assign each one given the random number. Then you can run System.out.println(firstFruit + " " + secondFruit + " " + thirdFruit); It would be better practice and more concise to move the if-else chain into its own function, but that's probably coming in lesson 2.
Thank you so much, I have a working slot machine!!!! Too bad it doesnt give out real money. Its guys like you on here that really make me excited to keep going.
@StevenEck Just try not to rely on SO too much. A lot of learning in programming comes from banging your head against a problem a dozen different ways until the right solution hits you.
Thanks again. I fixed the ticker. I understand what you're saying about relying too much on help. I guess I have to start to get more comfortable in the idea that I can do it myself. Sometimes it seems so overwhelming but I love doing it. I suppose it's practice, practice, practice.
|
0

Create an array:

String[] s = {Cherries, Oranges, Plums, Bells, Melons, Bars};

Then you can print s[num-1] instead of num (where num is the random int). E.g. if your random int came out to be 2, print s[2-1] i.e. s[1] which will be Orange.

Comments

0

Here's an alternative solution to the question which I think follows best programming practices. This is probably even less allowed for your assignment than an array, and will be a dead giveaway that you got your answer on StackOverflow, but the problem would lend itself to using an enum type with an int->enum mapping:

enum Fruit {
   Cherries(1), 
   Oranges(2), 
   Plums(3), 
   Melons(4), 
   Bars(5);

   private static final Map<Integer, Fruit> lookupMap = new HashMap<Integer, Fruit>();
   static {
      for (Fruit fruit : Fruit.values()) {
         lookupMap.put(fruit.getLookup());
      }
   }

   static Fruit fromLookup(int lookup) {
      return lookupMap.get(lookup);
   }

   private final int lookup;       

   private Fruit(int lookup) {
      this.lookup = lookup;
   }

   int getLookup() {
      return lookup;
   }
}

void printEnumExample() {
   int fruitToPrint = 4;
   System.out.println(Fruit.fromLookup(fruitToPrint)); // <- This will print "Melons"
}

1 Comment

Yeah, I imagine thats next semester stuff :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.