In: Computer Science
JAVA PROGRAM, Create the game "Rock, Scissor, Paper":
Dear student,
The below code is for english language. for any other language add if statement for each print statement based language variable.
import java.util.Scanner;
import java.util.Random;
public class Main
{
public static void main(String[] args) {
int language=1;
int choice=0;
int computerChoice=0;
int winOrLose=0;
int PlayerWins=0;
int ComputerWins=0;
int continueGame=0;
System.out.println("Welcome");
System.out.println("Press 1 for
english \nPress 2 for French");
Scanner myObj = new
Scanner(System.in);
Random rand = new Random();
language = myObj.nextInt();
while(language !=
1&&language!=2)
{
System.out.println("Press 1 for
english \nPress 2 for French");
language = myObj.nextInt();
}
while(true)
{
System.out.println("Please choose
Rock Paper Scissor");
System.out.println("Press 1 for
Rock");
System.out.println("Press 2 for
Paper");
System.out.println("Press 3 for
Scissor");
choice = myObj.nextInt();
while(choice !=
1&&choice!=2&&choice!=3)
{
System.out.println("Press 1 for
Rock");
System.out.println("Press 2 for
Paper");
System.out.println("Press 3 for
Scissor");
choice = myObj.nextInt();
}
computerChoice = rand.nextInt(3) + 1;
System.out.println("Computer chose "+computerChoice);
if(choice==computerChoice)
{
System.out.println("Draw");
winOrLose=0;
}
else
if((choice==1&&computerChoice==2)||(choice==2&&computerChoice==3)||(computerChoice==1&&choice==3))
{
System.out.println("Computer
Wins");
winOrLose=2;
}
else
if((computerChoice==1&&choice==2)||(computerChoice==2&&choice==3)||(choice==1&&computerChoice==3))
{
System.out.println("Player
Wins");
winOrLose=1;
}
if(winOrLose==1)
PlayerWins++;
else if(winOrLose==2)
ComputerWins++;
if(winOrLose!=0)
{
System.out.println("\n\npress 1 to
continue press 2 to exit");
continueGame =
myObj.nextInt();
}
if(continueGame==2)
{
break;
}
}
System.out.println("Player Total
wins = "+PlayerWins);
System.out.println("Computer Total
wins = "+ComputerWins);
}
}
Screenshot of output
If you have any doubt please comment below also dont forget to hit the like button thank you :)