Untitled
unknown
plain_text
21 days ago
2.5 kB
4
Indexable
Never
package rockpaperscissors2; import java.util.Scanner; import static java.lang.Math.random; public class RockPaperScissors2 { public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] choices = {"paper", "scissors", "rock"}; int[] coinRewards = new int[10]; // Array to store coins for 10 rounds int totalCoins = 0; for (int round = 0; round < 10; round++) { // random index (for the computer) int rand = (int) (random() * 3); System.out.print("Enter your choice (paper/scissors/rock): "); String input = in.nextLine(); System.out.printf("The computer choice is: %s\n", choices[rand]); int idx = rand; for (int i = 0; i < 3; i++) { if (i == rand) continue; if (compareStrings(input, choices[i])) idx = i; } if (rand - idx == -1 || rand - idx == 2) { System.out.println("The player wins"); // Generate 2 random numbers as the coin rewards int a = (int) (Math.random() * 10); int b = (int) (Math.random() * 10); int sum = Math.addExact(a, b); // Store the coin reward in the array and update total coins coinRewards[round] = sum; totalCoins += sum; System.out.println("Congratulations, you have won " + sum + " coins!"); System.out.println("Total coins so far: " + totalCoins); } else if (rand == idx) { System.out.println("It's a tie"); } else { System.out.println("The Computer wins"); System.out.println("Oh no! Try again."); } System.out.println("Round " + (round + 1) + " complete."); } // Display total coins won at the end of the game System.out.println("Game over! You played 10 rounds."); System.out.println("Total coins won: " + totalCoins); // Optional: Show the coins won in each round System.out.println("Coins won in each round:"); for (int i = 0; i < 10; i++) { System.out.println("Round " + (i + 1) + ": " + coinRewards[i] + " coins"); } } public static boolean compareStrings(String a, String b) { return a.equalsIgnoreCase(b); // Simple string comparison ignoring case } }
Leave a Comment