简体   繁体   中英

Trouble with nested if - else statements

I am very new at coding and attempted to make a rock paper scissors code based on one I saw on here. However, when the system is supposed to output the result of the game after in prints what the computer played, it just does not print. Any ideas? Thanks!

import java.util.Scanner;
import java.util.Random;

public class Rock {

    public static void main(String[] args) {  

        Scanner scan = new Scanner(System.in);
        Random gen = new Random();

        System.out.println("Hey, let's play Rock, Paper, Scissors!\n" + 
                   "Please enter a move.\n" + "Rock = R, Paper" + 
                   "= P, and Scissors = S.");

        int computerInt = gen.nextInt(3)+1;

        String computerPlay = "";
        if (computerInt == 1)
            computerPlay = "R";
        else if (computerInt == 2)
            computerPlay = "P";
        else if (computerInt == 3)
            computerPlay = "P";

        System.out.print("Please enter your play: ");

        String personPlay = scan.next();
        personPlay = personPlay.toUpperCase();
        System.out.println("Computer play is: " + computerPlay);

        if (personPlay.equals(computerPlay)) 
           System.out.println("It's a tie!"); 
        else if (personPlay.equals("R")) 
             if (computerPlay.equals("S")) 
              System.out.println("Rock crushes scissors. You win!!");
        else if (computerPlay.equals("P")) 
                System.out.println("Paper eats rock. You lose!!"); 
        else if (personPlay.equals("P")) 
           if (computerPlay.equals("S")) 
           System.out.println("Scissor cuts paper. You lose!!"); 
        else if (computerPlay.equals("R")) 
                System.out.println("Paper eats rock. You win!!"); 
        else if (personPlay.equals("S")) 
             if (computerPlay.equals("P")) 
             System.out.println("Scissor cuts paper. You win!!"); 
        else if (computerPlay.equals("R")) 
                System.out.println("Rock breaks scissors. You lose!!"); 
        else 
             System.out.println("Invalid user input.");
     }
}

The nesting of your if statements is totally wrong. Your indentation (kind of almost) hints at how you wish your code be executed, but the compiler does not care at all about your indentation, it only obeys the rules of the java language.

Multiple nested conditional statements like if() else if() if() are notoriously hard to determine (by a human) how they will be executed.

So: never use more than one statement without curly braces. (Some even say to always use curly braces, even if you have only one statement.)

When there is even the slightest ambiguity, (as there is in the code you have written,) make sure to always add curly braces to ensure that the compiler will compile your code the way you intend it to.

Then, your code will (probably) work.

The two big things in coding are making your code both easy to read and functional. Those if statements do not fulfill either of those needs. Also you do not use curly braces... Use curly braces {} ! This is your code but I have organised it to be a little more clean. Also I honestly don't know exactly why it didn't print in your original code, but that is not the problem. This one works.

import java.util.Random;
import java.util.Scanner;

public class RockPaperScissors {
    private static Scanner scan = new Scanner(System.in);

    public static void main(String[] args) {
        String personPlay;
        String computerPlay = "";
        int computerInt;

        Random gen = new Random();

        System.out.println("Hey, let's play Rock, Paper, Scissors!\n" + "Please enter a move.\n" + "Rock = R, Paper"
                + "= P, and Scissors = S.");

        computerInt = gen.nextInt(3) + 1;

        if (computerInt == 1) {
            computerPlay = "R";
        }
        if (computerInt == 2) {
            computerPlay = "P";
        }
        if (computerInt == 3) {
            computerPlay = "P";
        }

        System.out.print("Please enter your play: ");

        personPlay = scan.next();

        personPlay = personPlay.toUpperCase();

        System.out.println("Computer play is: " + computerPlay);
        if (personPlay.equals(computerPlay)) {
            System.out.println("It's a tie!");
        }
        if (personPlay.equals("R") && computerPlay.equals("S")) {
            System.out.println("Rock crushes scissors. You win!!");
        }
        if (personPlay.equals("R") && computerPlay.equals("P")) {
            System.out.println("Paper eats rock. You lose!!");
        }
        if (personPlay.equals("P") && computerPlay.equals("R")) {
            System.out.println("Paper eats rock. You win!!");
        }
        if (personPlay.equals("P") && computerPlay.equals("S")) {
            System.out.println("Scissor cuts paper. You lose!!");
        }
        if (personPlay.equals("S") && computerPlay.equals("R")) {
            System.out.println("Rock breaks scissors. You lose!!");
        }
        if (personPlay.equals("S") && computerPlay.equals("P")) {
            System.out.println("Scissor cuts paper. You win!!");
        }
        if (!computerPlay.equals("R") && !computerPlay.equals("P") && !computerPlay.equals("S")) {
            System.out.println("Invalid user input.");
        }

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM