dimanche 1 mars 2015

Java Slot Machine

Okay so I've been trying to do this assignment for my intro to java script class for the past week. I was assigned to do a program a slot machine that'll ask for your first and last name, then ask for the amount you want to bet. The program will then randomly generate 3 of the following 5 words: Cherries, Oranges, Plums, Bells, Melons, Bars. If all 3 match, then the Java file will give you 3 times the amount you bet. If two only match, then just 2 times the amount. And if none match, then the program will tell you "You didn't win anything." Here's two links to the full assignment description if you want to look at that: link 1 link 2 The problem I'm having is that I can't seem to get the code to display if you win or lose corrently. Sometimes it will multiply the winnings when the words do not completely match. Here's the code so far:


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


public class SlotMachine {



public static void main(String[] args) {
Scanner input = new Scanner(System.in);

String Ch = "Cherries";
String Or = "Oranges";
String Pl = "Plums";
String Be = "Bells";
String Me = "Melons";
String Ba = "Bars";


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

System.out.print("Enter your last name: ");
String LastName = input.nextLine();
System.out.println("Enter your first name: ");
String FirstName = input.nextLine();
System.out.print("Okay, so your name is " +LastName+ ", " +FirstName+
". Now please enter the amount you want to bet:");
int YourBet = input.nextInt();
System.out.print("Lets Begin. -------------->Your Bet is: $" +YourBet);


int results = 0;
int fruits = 0;

for (results = 0; results <= 2; results++) {

int number = (int)( Math.random() *6);
int number2 = (int)( Math.random() *6);
int number3 = (int)(Math.random() *6);

if(number == 0|| number2==0 ||number3==0) {
System.out.printf("\n"+Ch);
fruits = 0;
}
else if (number == 1|| number2 == 1|| number3 == 1) {
System.out.printf("\n"+Or);
fruits = 1;
}
else if (number == 2|| number2 == 2|| number3 == 2) {
System.out.printf("\n"+Pl);
fruits = 2;
}
else if (number == 3||number2 == 3|| number3 == 3) {
System.out.printf("\n"+Be);
fruits = 3;
}
else if (number == 4|| number2 == 4|| number3 == 4 ) {
System.out.printf("\n"+Me);
fruits = 4;
}
else if (number == 5|| number2 == 5|| number3 == 5) {
System.out.printf("\n"+Ba);
fruits = 5;
}
if (number == fruits && number2 == fruits && number == fruits ) {

System.out.print("\nYou Won $" +YourBet *3);
}
else if (number == fruits && number2 == fruits || number == fruits && number3 == fruits) {

System.out.print("\nYou Won $" +YourBet *2);
}
else if (number != fruits && number2 != fruits && number != fruits ) {
System.out.print("\nYou didn't win anything.");
}

}






}


}




Aucun commentaire:

Enregistrer un commentaire