2 Replies - 4927 Views - Last Post: 04 October 2012 - 03:04 PM Rate Topic: -----

#1 jmart  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 29-September 12

Help with slot machine simulation

Posted 04 October 2012 - 01:04 PM

Design a program that simulates a slot machine. When the program runs, it should do the following:
- Ask the user to enter the amount of money he or she wants to insert into the slot machine
- Instead of displaying images, the program will randomly select a word from the following list:
Cherries, Oranges, Plums, Bells, Melons, Bars
The program will select and display a word from this list three times.
- If none of the randomly selected words match, the program will inform the user that he or she has won $0. If two of the words match, the program will inform the user that he or she has won two times the amount entered. If three of the words match, the program will inform the user that he or she has won three times the amount entered.
- The program will ask whether the user wants to play again. If so, these steps are repeated. If not, the program displays the total amount of money entered into the slot machine and the total amount won.
----------------------------------------------------------------------------------------------------------------------
I can't seem to figure out how to get it to repeat and display the amount they have won after they enter their bet. Any help would be great. Thank you in advance.

This is what I have so far:
import java.util.Random;
import java.util.Scanner;

public class SlotMachine
{
	public static void main(String[] args)
	{
	// Create a Random object.  This object will
	// provide us with the random integer.
	Random randomIntegers = new Random();
	Scanner keyboard = new Scanner(System.in);
	
	// Declare variables
	int Cherries = 0;
	int Oranges	= 1;
	int Plums = 2;
	int Bells = 3;
	int Melons = 4;
	int Bars = 5;
	int counter;
	int bet;
	int firstChoice;
	int secondChoice;
	int thirdChoice;
	
	// Prompt user for bet
	System.out.println("Enter your bet amount");
	bet = keyboard.nextInt();
	System.out.println("Enter your first choice");
	firstChoice = keyboard.nextInt();
	System.out.println("Enter your second choice");
	secondChoice = keyboard.nextInt();
	System.out.println("Enter your third choice");
	thirdChoice = keyboard.nextInt();
	
	for (counter = 1; counter <=3; counter++)
	// Perform the selected operation.
	switch(randomIntegers.nextInt(5))
		{		
		case 0:
		System.out.println("Cherry"+ "\t" + "=$" + 0);
		break;
		case 1:
		System.out.println("Orange"+ "\t" + "=$" + 1);
		break;
		case 2:
		System.out.println("Plum"+ "\t" + "=$" + 2);
		break;
		case 3:
		System.out.println("Bell"+ "\t" + "=$" + 3);
		break;
		case 4:
		System.out.println("Melon"+ "\t" + "=$" + 4);
		break;
		case 5:
		System.out.println("Bar"+ "\t" + "=$" + 5);
		break;
		
		}
	}
}
		



Is This A Good Question/Topic? 0
  • +

Replies To: Help with slot machine simulation

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: Help with slot machine simulation

Posted 04 October 2012 - 01:14 PM

So you need a loop to surround the code that gets the bet, plays the game, calculates the winnings/losses and the amount remaining, and asks if another play is desired.

Often a do/while loop is used in this case:
// set up the initial conditions
do
{
   // play the game (do the above listed stuff)
   // ask the user if another play is desired
   // and set a boolean flag

   // if user wants to playAgain
   // playAgain = true
   // else playAgain = false
}
while ( playAgain );

Was This Post Helpful? 0
  • +
  • -

#3 jmart  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 29-September 12

Re: Help with slot machine simulation

Posted 04 October 2012 - 03:04 PM

I am pretty lost and confused. I am still a beginner so sorry. I've added some more code. Also I need the winnings to be the users bet multiplied by the number of matches(up the 3 matches). Can someone walk me through this.

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

public class SlotMachine
{
	public static void main(String[] args)
	{
	// Create a Random object.  This object will
	// provide us with the random integer.
	Random randomIntegers = new Random();
	Scanner keyboard = new Scanner(System.in);
	
	// Declare variables
	int Cherries = 0;
	int Oranges	= 1;
	int Plums = 2;
	int Bells = 3;
	int Melons = 4;
	int Bars = 5;
	int counter;
	int bet;
	int firstChoice;
	int secondChoice;
	int thirdChoice;
	int playAgain;
	
	// Prompt user for bet
	System.out.println("Enter your bet amount");
	bet = keyboard.nextInt();
	System.out.println("Enter your first choice");
	firstChoice = keyboard.nextInt();
	System.out.println("Enter your second choice");
	secondChoice = keyboard.nextInt();
	System.out.println("Enter your third choice");
	thirdChoice = keyboard.nextInt();
	
	
	for (counter = 1; counter <=3; counter++)
	// Perform the selected operation.
	switch(randomIntegers.nextInt(5))
		{		
		case 0:
		System.out.println("Cherry"+ "\t" + "=$" + 0);
		break;
		case 1:
		System.out.println("Orange"+ "\t" + "=$" + 1);
		break;
		case 2:
		System.out.println("Plum"+ "\t" + "=$" + 2);
		break;
		case 3:
		System.out.println("Bell"+ "\t" + "=$" + 3);
		break;
		case 4:
		System.out.println("Melon"+ "\t" + "=$" + 4);
		break;
		case 5:
		System.out.println("Bar"+ "\t" + "=$" + 5);
		break;
		
	
			// Prompt user if they want to play again
			System.out.println("Would you like to enter another bet?");
			System.out.println("Enter y for yes or n for no.");
			if (playAgain.equals("y"))
				
			if else
				(playAgain.equal("n"));
				System.out.print("Thank you for playing.");
				System.out.print("Your total winnings is :$");
			
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1