Final answer:
To create a Rock-Paper-Scissors program in Java, you can use random number generation to represent the computer's choice and accept user input for their choice. The program compares the choices to determine the winner based on the rules of the game and declares a winner or a tie at the end of 5 rounds.
Step-by-step explanation:
To create a Rock-Paper-Scissors program in Java, you can use random number generation to represent the computer's choice and accept user input for their choice. Here's an example code:
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
public static void main(String[] args) {
String[] choices = {"rock", "paper", "scissors"};
Random random = new Random();
int computerChoice = random.nextInt(3);
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your choice (rock, paper, or scissors): ");
String userChoice = scanner.nextLine().toLowerCase();
System.out.println("Computer chose: " + choices[computerChoice]);
System.out.println("You chose: " + userChoice);
if (userChoice.equals(choices[computerChoice])) {
System.out.println("It's a tie!");
} else if ((userChoice.equals("rock") && choices[computerChoice].equals("scissors")) ||
(userChoice.equals("paper") && choices[computerChoice].equals("rock"))) {
System.out.println("You win!");
} else {
System.out.println("Computer wins!");
}
}
}
This program allows the user to play against the computer by entering their choice (rock, paper, or scissors) and then determines the winner based on the rules of the game. It uses random number generation to represent the computer's choice, and it compares the choices to determine the winner. The program repeats this process for 5 rounds and declares a winner or a tie at the end.