In: Computer Science
Java - Create 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 enter into the slot machine. - Instead of displaying images, have the program 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 through 5. If the number is 0, the selected word is Cherries, if the number is 1, the selected word is Oranges, and so forth. The program should randomly select a word from the list three times and display all three of the words.) - If none of the randomly selected words match, the program informs the user that he or she has won $0. If two of the words match, the program informs the user that he or she has won two times the amount entered. If three of the words match, the program informs the user that he or she has won three times the amount entered. - 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.
If you could just take a screen shot of the actual code on java that would be great. A lot of the characters appear differently when answer is copied and pasted on answer forum.
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
// SlotMachine.java
import java.util.Random;
import java.util.Scanner;
public class SlotMachine {
public static void main(String[] args) {
//Declaring variables
char ch;
int totalWon=0,totalEntered=0,money,won=0;
String randWord1,randWord2,randWord3;
int randNum;
//Creating an Array
String
str[]={"Cherries","Oranges","Plums","Bells","Melons","Bars"};
//Creating an random class object
Random r = new Random();
/*
* Creating an Scanner class
object which is used to get the inputs
* entered by the user
*/
Scanner sc = new
Scanner(System.in);
/* This while loop continues to execute
* until the user enters either 'N' or 'n'
*/
while(true)
{
won=0;
System.out.print("\nEnter the amount of money you want to enter
:$");
money=sc.nextInt();
totalEntered+=money;
randNum=r.nextInt(6);
randWord1=str[randNum];
randNum=r.nextInt(6);
randWord2=str[randNum];
randNum=r.nextInt(6);
randWord3=str[randNum];
int
cnt=countSameWords(randWord1,randWord2,randWord3);
System.out.println("No of words matched :"+cnt);
if(cnt==0)
{
totalWon+=0;
won+=0;
}
else
if(cnt==2)
{
totalWon+=2*money;
won+=2*money;
}
else
if(cnt==3)
{
totalWon+=3*money;
won+=3*money;
}
System.out.println("You won amount :$"+won);
System.out.print("\nDo you
want to play again (Y/N) ::");
ch =
sc.next(".").charAt(0);
if(ch=='Y'||ch=='y')
continue;
else
{
System.out.println("::
Program Exit ::");
break;
}
}
System.out.println("\nTotal
Entered amount :$"+totalEntered);
System.out.println("Total Won
amount :$"+totalWon);
}
private static int countSameWords(String randWord1,
String randWord2,
String
randWord3) {
if(randWord1.equals(randWord2)
&& randWord2.equals(randWord3))
return 3;
else if(randWord1.equals(randWord2)
|| randWord2.equals(randWord3))
return 2;
else
return 0;
}
}
_____________________
Output:
Enter the amount of money you want to enter :$100
No of words matched :2
You won amount :$200
Do you want to play again (Y/N) ::y
Enter the amount of money you want to enter
:$50
No of words matched :2
You won amount :$100
Do you want to play again (Y/N) ::n
:: Program Exit ::
Total Entered amount :$150
Total Won amount :$300
_______________Could you plz rate me well.Thank You