Answer:
This Should work
Step-by-step explanation:
import java.util.*;
public class SlotMachine {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int money = 0;
int gamesPlayed = 0;
int amountWon = 0;
// Display welcome message and ask user to insert money
System.out.println("Welcome to the Slot Machine! Please insert some money to begin.");
System.out.print("Enter amount: $");
money = input.nextInt();
// Start the game loop
while (true) {
System.out.println("Press enter to spin the reels...");
input.nextLine(); // Wait for user to press enter
gamesPlayed++; // Increment games played
// Generate three random words
String[] words = {"Cherry", "Orange", "Plum", "Melon", "Bell", "Bar"};
String word1 = words[new Random().nextInt(words.length)];
String word2 = words[new Random().nextInt(words.length)];
String word3 = words[new Random().nextInt(words.length)];
// Display the words
System.out.println(word1 + " | " + word2 + " | " + word3);
// Determine if the user won
if (word1.equals(word2) && word2.equals(word3)) {
// All three words match - user wins large prize
System.out.println("Congratulations! You won the large prize!");
amountWon += money * 10; // Add 10 times the amount bet to winnings
} else if (word1.equals(word2) || word1.equals(word3) || word2.equals(word3)) {
// Any two words match - user wins small prize
System.out.println("Congratulations! You won the small prize!");
amountWon += money * 2; // Add 2 times the amount bet to winnings
} else {
// No words match - user loses
System.out.println("Sorry, you did not win this time.");
amountWon -= money; // Subtract amount bet from winnings
}
// Display current winnings and ask if the user wants to play again
System.out.println("Current winnings: $" + amountWon);
System.out.print("Play again? (Y/N): ");
String playAgain = input.nextLine();
if (!playAgain.equalsIgnoreCase("Y")) {
break; // Exit game loop if user does not want to play again
}
}
// Display total amount of money entered and games played, and amount won or lost
System.out.println("Thanks for playing!");
System.out.println("Total money entered: $" + (gamesPlayed * money));
System.out.println("Total games played: " + gamesPlayed);
System.out.println("Amount won or lost: $" + amountWon);
}
}