55.8k views
1 vote
Design a Java program that simulates a slot machine. When the program runs, it should do the following:

Ask the user to insert an amount of money / begin the game.
Randomly select three words from the following list (or a similar list): "Cherry", "Orange", "Plum", "Melon", "Bell", "Bar"
Display the randomly selected words. For example: Cherry | Melon | Bell
Determine if the user won:
If no words match, the user loses.
If any two words match, the user wins a small prize.
If all three words match, the user wins a large prize.
Ask if the user would like to play again. If so, the above steps are repeated. If the user does not wish to play again, display the total amount of money entered into the slot machine / number of games played / some metric to represent the games played and also display the amount of money won or lost.
There is room to design your slot machine as you wish, but the core functionality of steps 1, 3, 4, and 5 must be present.

1 Answer

6 votes

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);

}

}

User Gdurelle
by
8.4k points