Answer:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random r = new Random();
int computer = r.nextInt(3);
System.out.print("Enter a number[0-1-2]: ");
int user = input.nextInt();
switch(computer){
case 0:
if(user == 0)
System.out.println("It is a draw");
else if(user == 1)
System.out.println("User wins");
else
System.out.println("Computer wins");
break;
case 1:
if(user == 0)
System.out.println("Computer wins");
else if(user == 1)
System.out.println("It is a draw");
else
System.out.println("User wins");
break;
case 2:
if(user == 0)
System.out.println("User wins");
else if(user == 1)
System.out.println("Computer wins");
else
System.out.println("It is a draw");
break;
}
}
}
Step-by-step explanation:
Create objects to use the Scanner and Random class
Using the nextInt() method with Random object r, generate a random number between 0 to 2 and set it to the variable named computer
Ask the user to enter a number using Scanner object and nextInt() method and set it to the variable named user
Check the value of computer variable using switch statement.
When computer is 0, check the value of the user variable using if statement. If user is 0, it is a draw. If user is 1, the user wins. Otherwise, the computer wins.
When computer is 1, check the value of the user variable using if statement. If user is 0, the computer wins. If user is 1, it is a draw. Otherwise, the user wins.
When computer is 2, check the value of the user variable using if statement. If user is 0, the user wins. If user is 1, the computer wins. Otherwise, it is a draw.