Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Inserting an if/else or switch.

$
0
0
This is the assignment.
Create a Java program that simulates a slot machine. When the program runs it should do the following:
1. Asks the user to enter the amount of money he or she wishes to enter into the slot machine.
2. Instead of displaying images, have the program display the name of the image. The program must randomly select a word from the following list:
Cherries, Oranges, Plums, Bells, Melons, Bars
To select a word, the program can generate a random number in the range of 0 to 5. If the number is 0, the selected word is Cherries, if the number is 1, then the selected word is Oranges, and so forth. The program should randomly select a word from the list 3 times and display all 3 of the words.
3. If none of the randomly selected words match, the program informs the user that he or she has won $0. If 2 of the words match, the program informs the user of the amount (2 times the amount entered) that he or she has won. If 3 of the words match, the program informs the user of the amount (3 times the amount entered) that he or she has won.
4. The program asks if 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 am lost with setting/connecting the random integers to the words. I have searched online and found similar programs although none with two classes.
I have asked on Yahoo Answers although not very familiar with Arrays yet. This is what the other person had replied with.

 Array the Slots
String[] slots = "Cherries, Oranges, Plums, Bells, Melons, Bars".split(", ");
String slot = slots[ rand.nextInt( slots.length ) ];
or
int index = rand.nextInt( 100000 ) % slots.length;



Do i need to insert the values into my driver such as
 import java.util.*;

public class Slot
{
int Cherries = 0;
int Oranges = 1;
int Plums = 2;
int Bells = 3;
int Melons = 4;
int Bars = 5;


along with getters/setters?

this is what I have started...
 import java.util.*;

public class Machine

{

public static void main (String[] args)

{

int slot1;
int slot2;
int slot3;
int usersBet;
String decision;

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

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

slot1 = rand.nextInt(5)+1;
slot2 = rand.nextInt(5)+1;
slot3 = rand.nextInt(5)+1;


System.out.println("Do you wish to play the slot machine?");
decision = scan.nextLine();

	
System.out.println("random numbers are " + slot1 + slot2 + slot3);
//just an example to make sure my random works

if (slot1 == slot2 && slot2 == slot3){
System.out.println (+slot1+ " | "+slot2+ " | "+slot3+"Winner!");
//etc etc although since I am using a driver I would not have it here?


} else if (slot1 == slot2 || slot1 == slot3 || slot2 == slot3) {
System.out.println("What is your bet?")
usersBet = scan.nextInt();

}
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>