Answer:
The code is written in the explanation.
Step-by-step explanation:
the code is the following:
import java.util.*;
class Game{
}
public class Main
{
static scanner in = new Scanner(System.in);
public static final String Rock = "Rock";
public static final String Paper = "Paper";
public static final String Scissors = "Scissors";
/*Get computer's move using Random
class nextInt() method */
public static string getcomputermove()
{
string computermove;
Random random = new Random();
int input = random.nextInt(3)+1;
if (input == 1)
computermove = Main.Rock;
else if(input == 2)
computermove = Main.Paper;
else
computermove = Main.Scissors;
System.out.println("computer move is: " + computermove);
System.out.println();
return computermove;
}
/* get player's move using scanner
class */
public static String getplayermove()
{
String input = in.next();
String playermove = input.toUpperCase();
System.out.println("player move is: "+ playermove);
return playermove;
}
public static void main(String args[])
{
System.out.println("welcome to rock, paper, scissors");
System.out.println("enter number of winning rounds");
int r=in.nextInt();
int playerScore=0;
int computerScore=0;
while(playerScore!=r && computerScore!=r){
System.out.println("enter any one of the following inputs: ");
System.out.println("Rock");
System.out.println("Paper");
System.out.println("Scissors");
System.out.println();
String playerMove = Main.getplayermove();
String computerMove = Main.getcomputermove();
//rules of the game applied:
/*if both playermove and computermove
produces the same selection, then
game is a tie*/
if (playermove.equals(computermove))
System.out.println("Game is tie");
// if playermove is Rock
else if (playerMove.equals(Main.Rock))
if(computerMove.equals(Main.Paper)){
System.out.println("computer wins");
computerscore=1;
}else{
System.out.println("player wins");
playerscore=1;
}
// if playermove is Paper
else if (playermove.equals(Main.Paper)){
if(computermove.equals(Main.Scissors)){
System.out.println("computer wins");
computerscore=1;
}else{
System.out.println("player wins");
playerscore=1;
}
}
// if playerMove is Scissors
else{
if(computermove.equals(Main.Rock)){
System.out.println("computer wins");
computerscore=1;
}else{
System.out.println("player wins");
playerscore=1;
}
}
System.out.println("After the round, the computer score is:"+computerscore+" and the player score is:"+playerscore);
}
if(computerscore==r){
System.out.println("You lose, computer wins");
}else{
System.out.println("You Won");
}
}
}