4 Replies - 4738 Views - Last Post: 27 October 2012 - 02:14 PM Rate Topic: -----

#1 GusTheo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-October 12

New to Java. Question about my Slot machine program.

Posted 27 October 2012 - 01:27 PM

Hey guys, I'm new to Java and am working on a slot machine program. I've got the legwork of it done, but I'm stumped at one of the parts. There are 3 different columns, with 5 different options for each. I used a random number generator (0-5) for them. My professor wants us to associate 0 with Cherries, 1 with Oranges, etc.

I have NO idea how to do this. I'm thinking I'm supposed to assign each word with that number, but I don't know how to do that (if at all possible) I've posted my code below. We've gone over classes, if statements and loops. We'v yet to cover arrays, so that isn't an option for me right now.

public class Slots 
{

    
    public static void main(String[] args) 
    {
        // Creating a Scanner object
        Scanner input = new Scanner(System.in);
        
        // Declare my variables as INTs
        int won = 0, bet = 0, totalBet = 0, totalWon = 0, counter = 0;
        
        // Declare my slots as INTs
        int num1, num2, num3;
        
      
        
        // Ask user how much they'd like to bet.
        System.out.println("How much would you like to bet? Bet 0 if " + 
                             "you wish to walk away.");
        bet = input.nextInt();
        
 
        // While  loop
        while (bet != 0)
        {
            totalBet += bet;
            totalWon += won; 
        
        // Create Random objct
        Random randomNumbers = new Random();
        
        num1 = randomNumbers.nextInt(5);
        num2 = randomNumbers.nextInt(5);
        num3 = randomNumbers.nextInt(5);
        
        // Show the numbers
        System.out.println("The three are " + num1 + " " + num2 +
                " " + num3 + " " );
        
        
        // If statement to decide if they won
        if ((num1 != num2 && (num1 != num3)))
            {
          System.out.println("Sorry you didn't win.");
            }   
        else if (num1 == num2 && num1 == num3)
            {
            won = bet * 3;
            System.out.println("You won " + (bet * 3));
            }
        else
            {
            System.out.println("You won " + (bet * 2));
            won = bet * 2;
            }       
        

        // Ask user how much they'd like to bet.
        System.out.println("How much would you like to bet? Bet 0 if" + 
                           "you wish to walk away.");
        bet = input.nextInt();
        
        }
         
        //Display totals
        System.out.println("Your total winnings are, " + totalWon +
                           " and your total bets were " + totalBet + ".");
        
    }
}



Help is greatly appreciated folks!

Is This A Good Question/Topic? 0
  • +

Replies To: New to Java. Question about my Slot machine program.

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: New to Java. Question about my Slot machine program.

Posted 27 October 2012 - 01:46 PM

Can you use switch? If not, then a simple if/else trail should suffice.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: New to Java. Question about my Slot machine program.

Posted 27 October 2012 - 01:56 PM

You can always use an array and random;y select from it

    Random ran = new Random();
    String[] figure = {"Sherry", "Orange", "Plum", "Apple", "Melon");

    String[] slot = new String[3];
    for(int i = 0; i < slot.lengh; ++i) {
        int selected = ran.nextInt(figure.length);
        slot[i] = figure[selected];
    }


Was This Post Helpful? 0
  • +
  • -

#4 GusTheo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-October 12

Re: New to Java. Question about my Slot machine program.

Posted 27 October 2012 - 02:06 PM

View PostGregBrannon, on 27 October 2012 - 01:46 PM, said:

Can you use switch? If not, then a simple if/else trail should suffice.


Yes we've gone over switch. I actually just found a code for a slot using the switch, I'm reading it now to try and understand it.

Out of curiosity, what is an if/else trail?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: New to Java. Question about my Slot machine program.

Posted 27 October 2012 - 02:14 PM

View PostGusTheo, on 27 October 2012 - 05:06 PM, said:

Out of curiosity, what is an if/else trail?


    // with a switch
    switch(n) {
        case 0:
          ...
          break;
        case 1:
          ...
          break;
        case 2:
          ...
          break;
        case 3:
          ...
          break;
     }

     // with if/else trail
     if(n == 0) {
        ...
     } else if(n == 1) {
        ...
     } else if(n == 2) {
        ...
     } else if(n == 3) {
        ...
     }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1