Your non-programmer friends have challenged your Java programming skills, and yo
ID: 3930366 • Letter: Y
Question
Your non-programmer friends have challenged your Java programming skills, and you are going to prove them wrong. 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.
Explanation / Answer
import java.util.Random;
import java.util.Scanner;
public class rockPaperScissors {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 1 - Rock 2 - Paper 3 - Scissors
// Anything will be beaten by its next one and will beat the previous one
Random rand = new Random();
Scanner scan = new Scanner(System.in);
int in,out,sout;
String con;
while(true)
{
int rn = rand.nextInt(2) - 1;
//System.out.println(randomNum);
System.out.println("Please select one of the following:");
System.out.println("1. Rock");
System.out.println("2. Paper");
System.out.println("3. Scissors");
System.out.print("Which one would you like to choose?");
in = scan.nextInt();
rn = rand.nextInt(2);
out = (in+rn);
sout=out;
if(sout>3)
{
sout = sout-3;
}
System.out.println("Computer's choice: "+sout);
if(in==out)
{
System.out.println("The Game is a tie.");
}
else if(out-in==1)
{
System.out.println("Computer won the Game.");
}
System.out.print("Would you like to continue? (Y/N)");
con = scan.next();
if(con.equals("N"))
{
break;
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.