Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to properly use both && and || in if statements?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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").

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading