Following an online tutorial, I wrote a program to develop a rock, paper, scissor game. Below I have elaborated the steps I took to write the code.
Step 1: Reading user input and saving in a String.
Step 2: Declared the computerChoice method to check the choice of computer using Math.random and save it as a string.
Step 3: Declared the result method to compare the user’s choice & computer’s choice and save the output as a string.
Step 4: Declared the printResult method to print the final outcome.
I have trouble at Step 3 where I have to compare the choices. I used an if statement in which there are both && and || operators. The output of the program is not consistent. Loose and tie scenarios are working properly, but win scenarios are not properly displayed.
What is the issue here? Thanks in advance.
import java.util.Scanner;
public class RockPaperScissors {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Let's play Rock Paper Scissors.");
System.out.println("When I say 'shoot', Choose: rock, paper, or scissors.\n");
System.out.println("Are you ready? Write 'yes' if you are.");
String answer = scan.nextLine(); //Ask for user input, if yes the game continues, if any other string input, game over
if (answer.equals("yes")) {
System.out.println("\nGreat!");
System.out.println("rock – paper – scissors, shoot!");
String userChoice = scan.nextLine(); //Reading user input and saving in a String
String comChoice = computerChoice (); // computerChoice method to check the choice of computer using Math.random and save it as a string
String finalResult = result(userChoice,comChoice); //result method to compare the userChoice & comChoice and save as a string
printResult(userChoice, comChoice, finalResult); // printResult method to print the final outcome
} else {
System.out.println("Darn, some other time...!");
}
scan.close();
}
public static String computerChoice () {
String comChoice="";
int randomInt=0;
double randomNumber = Math.random() * 3;
randomNumber+=1;
randomInt = (int) randomNumber;
if (randomInt==0) {
comChoice="rock";
} else if (randomInt==1) {
comChoice="paper";
} else {
comChoice="scissors";
}
return comChoice;
}
public static String result(String yourChoice, String computerChoice) {
String result = "";
if (yourChoice.equals("rock") && computerChoice.equals("Scissors") || yourChoice.equals("paper") && computerChoice.equals("rock") || yourChoice.equals("scissors") && computerChoice.equals("paper")) {
result = "You win";
} else if (computerChoice.equals("rock") && yourChoice.equals("scissors") || computerChoice.equals("paper") && yourChoice.equals("rock") || computerChoice.equals("scissors") && yourChoice.equals("paper")) {
result = "You loose";
} else if (yourChoice.equals(computerChoice)) {
result = "It's a tie";
}
return result;
}
public static void printResult(String yourChoice, String computerChoice, String result) {
System.out.println("\nYou chose: "+ yourChoice);
System.out.println("The computer chose: " + computerChoice);
System.out.println(result);
}
}
>Solution :
"Scissors" is not the same as "scissors".
if (yourChoice.equals("rock") && computerChoice.equals("Scissors") || yourChoice.equals("paper") && computerChoice.equals("rock") || yourChoice.equals("scissors") && computerChoice.equals("paper")) {
Be consistent in your use of capitals.
The important programming lesson here is "learn to read what your code actually says", not what you think it says. Everyone makes typos now and then, and it’s often extraordinarily difficult to see what’s right under your own nose.
(Also, the word you want for game-playing is "lose", not "loose").