7. (2 points) In this question you will write a function that determines the res
ID: 3757464 • Letter: 7
Question
7. (2 points) In this question you will write a function that determines the result of a rock, paper, scissors game given choices of player 1 and player 2. In particular, write a function rps winner) that prompts the user for choice of player 1 and then choice of player 2, and then it displays the result for player 1 as indicated in the examples given in Section 2. You may assume that the user will only enter words: rock, paper or scissors in lower case. Recall that paper beats rock, that rock beats scissors and that scissors beat paper. If both players make the same choice, we have a drawExplanation / Answer
public static String rps_winner()
{
// create Scanner class object to get user input
Scanner sc = new Scanner(System.in);
System.out.print("Player 1 choice : ");
// read word from user
String ch1 = sc.next();
System.out.print("Player 2 choice : ");
// read word from user
String ch2 = sc.next();
// itf its a draw
if( ch1.equalsIgnoreCase(ch2) )
System.out.println("It's a Draw.");
else
{
// if user 1 entered rock
if( ch1.equalsIgnoreCase("rock") )
{
// if user 2 entered paper
// Paper beats Rock
if( ch2.equalsIgnoreCase("paper") )
System.out.println("Player 2 Won!");
// if user 2 entered scissor
// Rock beats scissor
else
System.out.println("Player 1 Won!");
}
// if user 1 entered paper
else if( ch1.equalsIgnoreCase("paper") )
{
// if user 2 entered rock
// Paper beats Rock
if( ch2.equalsIgnoreCase("rock") )
System.out.println("Player 1 Won!");
// if user 2 entered scissor
// scissor beats paper
else
System.out.println("Player 2 Won!");
}
// if user 1 entered scissor
else
{
// if user 2 entered paper
// scissor beats paper
if( ch2.equalsIgnoreCase("paper") )
System.out.println("Player 1 Won!");
// if user 2 entered rock
// rock beats scissor
else
System.out.println("Player 2 Won!");
}
}
}
// in case of any issue, please leave a comment and I will resolve the issue
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.