Question

In: Computer Science

Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the...

Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the computer through a user interface. The user will choose to throw Rock, Paper or Scissors and the computer will randomly select between the two. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should then reveal the computer's choice and print a statement indicating if the user won, the computer won, or if it was a tie. Allow the user to continue playing until they chooses to stop. Once they stop, print the number of user wins, losses, and ties.

Solutions

Expert Solution

import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
class Main
{
        public static void main(String args[])
        {
                Scanner sc=new Scanner(System.in);

                int n;
                char ch;
                do {
                        System.out.println("Enter your choice \n1.Rock\n2.Sicssor\n3.Paper");
                        n=sc.nextInt();
                        int i=ThreadLocalRandom 
                                        .current() 
                                        .nextInt(1, 3 + 1); 

                        if(i==1)
                        {
                                System.out.println("The computer choice is Rock.");
                        }
                        else if(i==2)
                        {
                                System.out.println("The computer choice is Sicssor.");
                        }
                        else
                        {
                                System.out.println("The computer choice is Paper.");
                        }
                        if(n==1)
                        {
                                if(i==1)
                                {
                                        System.out.print("Its a tie.");
                                }
                                else if(i==2)
                                {
                                        System.out.print("You won the game.");
                                }
                                else
                                {
                                        System.out.print("Computer won the game.");
                                }
                        }
                        else if(n==2)
                        {
                                if(i==1)
                                {
                                        System.out.print("Computer won the game.");
                                }
                                else if(i==2)
                                {
                                        System.out.print("Its a tie.");
                                }
                                else
                                {
                                        System.out.print("You won the game.");
                                }
                        }
                        else
                        {
                                if(i==1)
                                {
                                        System.out.print("You won the game.");
                                }
                                else if(i==2)
                                {
                                        System.out.print("Computer won the game.");
                                }
                                else
                                {
                                        System.out.print("Its a tie");
                                }
                        }
                        System.out.print("\nDo you want to continue (Y/N): ");
                        ch=sc.next().charAt(0);
                }while(ch=='y'||ch=='Y');
        }
}

Here is the above program that asks the user to enter the choice of rock/paper/choice and then we the computer will generate the number by computer generator and then according to the question conditions we have  Rock beats Scissors, Scissors beats Paper, and Paper beats Rock.Then we will print who is the winner and then we ask for the user to continue or not is user enter y then we play game again or else we stop the game.

SCREENSHOT OF THE OUTPUT :


Related Solutions

Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: You can set these constant global variables at the top outside of your main function definition: COMPUTER_WINS = 1 PLAYER_WINS = 2 TIE = 0 INVALID = 3 ROCK = 1 PAPER = 2 SCISSORS = 3 For this program 1 represents rock, 2 represents paper, and 3 represents scissors. In...
Please use Python 21. Rock, Paper, Scissors Game Write a program that let's the user play...
Please use Python 21. Rock, Paper, Scissors Game Write a program that let's the user play the game of rock, paper, scissors against the computer. The program should work as follows: 1. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen...
Write a Java program that plays the game Rock, Paper, Scissors. The program should generate a...
Write a Java program that plays the game Rock, Paper, Scissors. The program should generate a random choice (Rock, Paper or Scissors) then ask the user to choose Rock, Paper or Scissors. After that the program will display its choice and a message showing if the player won, lost or tied. Next, the program should prompt the user to play again or not. Once the player selects to stop playing the game, the program should print the number of wins,...
Create a Java program that allows two players to play Rock, Paper, Scissors. Player 1 will...
Create a Java program that allows two players to play Rock, Paper, Scissors. Player 1 will enter an integer to determine whether they use rock, paper or scissors. Player 2 will also enter an integer to determine whether they use rock, paper or scissors. Use named constants for rock, paper and scissors and set their values as shown below. Use if-else statements to determine the results of the game. Use the named constants to compare with the player 1 and...
Subjects: Write a Python program that allows users to play the popular rock-paper-scissor game against the...
Subjects: Write a Python program that allows users to play the popular rock-paper-scissor game against the computer multiple times. This program assesses your knowledge of decision structures, repetition structures, and functions. Requirements: 1.Define a function named as play_game. This function receives two parameters representing the player’s and computer’s choices, and it returns an integer representing the game result from the player’s perspective. Specifically, it returns 1 if the player wins and 0 if the player does not win. Hint: you...
One file java program that will simulate a game of Rock, Paper, Scissors. One of the...
One file java program that will simulate a game of Rock, Paper, Scissors. One of the two players will be the computer. The program will start by asking how many winning rounds are needed to win the game. Each round will consist of you asking the user to pick between rock, paper, and scissors. Internally you will get the computers choice by using a random number generator. Rock beats Scissors, Paper beats Rock, and Scissors beats Paper. You will report...
Write a Java class that determines the winner of a rock, paper scissors game. Assume the...
Write a Java class that determines the winner of a rock, paper scissors game. Assume the input from the user is always valid (so no need to check), that is it contains either one of `R`, `P`, or `S` as a single character, or has matching parenthesis, like, `(S&P)` or `((R&P)&S)`, and the `&` character. So for example, the user inputs `(P&R)` and the program will output `P` since paper beats rock. Or if the user inputs `((S&R)&(S&S))` the output...
JAVA : Design and implement an application that plays the Rock-Paper-Scissors game against the computer. When...
JAVA : Design and implement an application that plays the Rock-Paper-Scissors game against the computer. When played between two people, each person picks one of three options (usually shown by a hand gesture) at the same time, and a winner is determined. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should randomly choose one of the three options (without revealing it) and then prompt for the user’s selection. At that point, the program...
write a python script for rock scissors paper game
write a python script for rock scissors paper game
1.Create full program in Java that will simulate a Rock Paper Scissors Game You will create...
1.Create full program in Java that will simulate a Rock Paper Scissors Game You will create the code so that when the program is run the user will see in the console the following: We are going to play rock paper scissors. Please choose 1 for Rock 2 for Paper or 3 for scissors. The program will have your choice which is the integer-valued typed in by the user and compChoice which will be the randomly generated value of either...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT