Question
Could someone please help me with this?

Thank you
Game Description: The popular rock-paper-scissors game is usually played between two people in which each player simultaneous
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Explanation:

Math.random() in java:

Math.random() is a method that returns double type where value is greater than or equal to 0.0 and it is less than 1.0

The following statement can be used to generate numbers between 1 and 3.

int randomNum=1+(int)(3*Math.random());

The numbers 1, 2 and 3 are generated randomly here.

The rock , paper , scissor are represented by 1, 2 and 3 respectively.

Program:

import java.lang.Math.*;
public class Main
{
   public static void main(String[] args) {
       int randomNum=0; //Declaration of variables
       int sum=0;
      
       for(int i=0;i<100;i++){ //loop repeates for 100 times
       randomNum=1+(int)(3*Math.random()); //generate random number
       if(i%3==0){ //System chooses scissor
       if(randomNum==1) //If random number is 1, it is rock and user wins.
       sum=sum+5;
       else if(randomNum==2) //If random number is 2, it means paper, then the user loses.
       sum=sum-1;
       else if(randomNum==3) //If random number is 3, it means scissors, then it is a tie.
       sum=sum+2;
       }
       else if(i%3==1){  //System chooses rock
       if(randomNum==1) //If random number is 1, it means rock, then it is a tie.
       sum=sum+2;
       else if(randomNum==2) //If random number is 2, it means paper, then the user wins.
       sum=sum+5;
       else if(randomNum==3) //If random number is 3, it means scissors, then the user loses.
       sum=sum-1;
       }
       else if(i%3==2){ //System chooses paper
       if(randomNum==1) //If random number is 1, it means rock, then the user loses.
       sum=sum-1;
       else if(randomNum==2) //If random number is 2, it means paper, then it is a tie.
       sum=sum+2;
       else if(randomNum==3) //If random number is 3, it means scissors, then the user wins.
       sum=sum+5;
       }
      
       }
       System.out.print("The total reward is ");
       System.out.println(sum);
   }
}

Explanation:

The loop is iterated for 100 items using iteration variable i.

When i%3==0 this means that computer chooses scissors to play.

Now, we check the random number generated.

If random number is 1, it means rock, then the user wins.

If random number is 2, it means paper, then the user loses.

If random number is 3, it means scissors, then it is a tie.

When i%3==1  this means that computer chooses rock to play.

Now, we check the random number generated.

If random number is 1, it means rock, then it is a tie.

If random number is 2, it means paper, then the user wins.

If random number is 3, it means scissors, then the user loses.

When i%3==2 this means that computer chooses paper to play.

Now, we check the random number generated.

If random number is 1, it means rock, then the user loses.

If random number is 2, it means paper, then it is a tie.

If random number is 3, it means scissors, then the user wins.

Screenshot:

The execution of the program is attached below along with the output. Please follow it for reference.

1 import java.lang. Math.; 2 public class Main 3 { public static void main(String[] args) { int randomNum=0; int sum=0; for(i

Main.java 27 28 29 if(randomNum=1) sum=sum-1; else if(randomNum==2) sum-sum+2; else if(randomNum==3) sum=sum+5; My many mommy

Add a comment
Know the answer?
Add Answer to:
Could someone please help me with this? Thank you Game Description: The popular rock-paper-scissors game is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of...

    java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of the common game Rock Paper Scissors that is often used to pass time (or sometimes to make decisions.) The rules of the game are outlined below: • • Scissors cuts Paper Paper covers Rock Rock crushes Lizard Lizard poisons Spock Spock smashes Scissors Scissors decapitates Lizard Lizard eats Paper Paper disproves Spock Spock vaporizes Rock Rock crushes Scissors Write a program that simulates the...

  • Using python 3.7.3 Challenge: Rock, Paper, Scissors GamePDF Description: Create a menu-driven rock, paper, scissors game...

    Using python 3.7.3 Challenge: Rock, Paper, Scissors GamePDF Description: Create a menu-driven rock, paper, scissors game in Python 3 that a user plays against the computer with the ability to save and load a game and its associated play statistics. Purpose: The purpose of this challenge is to assess the developer’s ability to create an interactive application with data persistence in Python 3. Requirements: Create a Rock, Paper, Scissors game in Python named rps.py according to the requirements specified in...

  • This program should be in c++. Rock Paper Scissors: This game is played by children and...

    This program should be in c++. Rock Paper Scissors: This game is played by children and adults and is popular all over the world. Apart from being a game played to pass time, the game is usually played in situations where something has to be chosen. It is similar in that way to other games like flipping the coin, throwing dice or drawing straws. There is no room for cheating or for knowing what the other person is going to...

  • Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that...

    Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that allows the user to play "Rock, Paper, Scissors". Write the RPS class. All code should be in one file. main() will be part of the RPS class, fairly small, and contain only a loop that asks the user if they want to play "Rock, Paper, Scissors". If they say yes, it calls the static method play() of the RPS class. If not, the program...

  • IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors...

    IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. 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 scissors. Don’t display the computer’s choice yet....

  • (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against...

    (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. 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 scissors. Don’t display the computer’s choice yet. The...

  • It's writing a simple rock paper scissors game strictly following the instructions. INSTRUCTIONS: If the user...

    It's writing a simple rock paper scissors game strictly following the instructions. INSTRUCTIONS: If the user selects 'p': 1. First the program should call a function named getComputerChoice to get the computer's choice in the game. The getComputerChoice function should generate a random number between 1 and 3. If the random number is 1 the computer has chosen Rock, if the random number is 2 the user has chosen Paper, and if the random number is 3 the computer has...

  • For this week’s assignment, we will be recreating the Rock, Paper, Scissors program using Object-...

    For this week’s assignment, we will be recreating the Rock, Paper, Scissors program using Object-Oriented Programming. You will be working with me on a team to build the program. I have already written my part of the program, and the Game.java file is attached. Your task will be to write a RockPaperScissors class that contains the following methods: getUserChoice: Has the user choose Rock, Paper, or Scissors. After validating the input, the method returns a String containing the user choice....

  • Rock, Paper, Scissors (also known by several other names, see http://en.wikipedia.org/wiki/Rock p...

    Help needed in Perl language program! Note : Perl Language Rock, Paper, Scissors (also known by several other names, see http://en.wikipedia.org/wiki/Rock paper scissors) is an extremely popular hand game most often played by children. Often, it is used as a method of selection similar to flipping a coin or throwing dice to randomly select a person for some purpose. Of course, this game is not truly random since a skilled player can often recognize and exploit the non-random behavior of...

  • “Oh I know, let’s play Rock, Paper, Scissors, Lizard, Spock. It’s very simple.” What better way...

    “Oh I know, let’s play Rock, Paper, Scissors, Lizard, Spock. It’s very simple.” What better way to expand the classic Rock, Paper, Scissors game than to add Lizard and Spock! Sheldon and Raj brought Sam Kass’s game to life on The Big Bang Theory in “The Lizard-Spock Expansion” episode.   Assignment & Discussion Your task in Programming Assignment 8 is to create a C# console app-version of Rock, Paper, Scissors, Lizard, Spock (RPSLS). Use any of the programming tools and techniques...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT