Write a program that will play rock, paper, scissors against them, but since we
ID: 646808 • Letter: W
Question
Write a program that will play rock, paper, scissors against them, but since we are annoyed at this challenge, write it in such a way that it can never lose. However, it must be able to tie so that they dont get TOO suspicious. In your main, ask the user to enter their choice - rock, paper or scissors. Write a method that given the user's choice, generates and returns the computer's choice. It should randomly pick the winning choice or tie the user's choice. As your output, show both the user's and the computer's choice and declare the winner or the tie. Keep asking the user if they want to play until they give up and you have claimed your superiority. (JAVA PROGRAM HELP)
Explanation / Answer
import java.util.Scanner;
import java.util.Random;
public class Rock
{
public static void main(String[] args)
{
String personPlay; //User's play -- "R", "P", or "S"
String computerPlay = ""; //Computer's play -- "R", "P", or "S"
int computerInt; //Randomly generated number used to determine
//computer's play
String response;
System.out.println("Hey, let's play Rock, Paper, Scissors! " +
"Please enter a move. " + "Rock = R, Paper" +
"= P, and Scissors = S.");
System.out.println();
while (true) {
Scanner scan = new Scanner(System.in);
Random generator = new Random();
//Generate computer's play (0,1,2)
computerInt = generator.nextInt(3)+1;
//Translate computer's randomly generated play to
//string using if //statements
if (computerInt == 1)
computerPlay = "R";
else if (computerInt == 2)
computerPlay = "P";
else if (computerInt == 3)
computerPlay = "S";
//Get player's play from input-- note that this is
// stored as a string
System.out.println("To exit, press q. Enter your play: ");
personPlay = scan.next();
//Make player's play uppercase for ease of comparison
personPlay = personPlay.toUpperCase();
if(personPlay.equals("Q")) {
System.out.println(" Exiting ");
return;
}
//See who won. Use nested ifs
if (personPlay.equals(computerPlay)) {
System.out.println("Computer play is: " + computerPlay);
System.out.println("It's a tie!");
}
else if (personPlay.equals("S"))
if (computerPlay.equals("P")) {
System.out.println("Computer play is: " + computerPlay);
System.out.println("You win!!");
}
else {
computerPlay = "S";
System.out.println("Computer play is: " + computerPlay);
System.out.println("It's a tie!");
}
else if (personPlay.equals("P"))
if (computerPlay.equals("R")) {
System.out.println("Computer play is: " + computerPlay);
System.out.println("You win!!");
}
else {
computerPlay = "P";
System.out.println("Computer play is: " + computerPlay);
System.out.println("It's a tie!");
}
else if (personPlay.equals("R"))
if (computerPlay.equals("S")) {
System.out.println("Computer play is: " + computerPlay);
System.out.println("You win!!");
}
else {
computerPlay = "R";
System.out.println("Computer play is: " + computerPlay);
System.out.println("It's a tie!");
}
else
System.out.println("Invalid user input.");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.