嵌套if - else语句有问题

时间:2016-10-01 01:06:09

标签: java

我是非常擅长编码,并试图根据我在这里看到的那个制作一个石头剪刀代码。但是,当系统在打印出计算机播放的内容后输出游戏结果时,它就不会打印出来。有任何想法吗?谢谢!

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

2 个答案:

答案 0 :(得分:1)

if语句的嵌套是完全错误的。您的缩进(几乎有点)暗示您希望如何执行代码,但编译器根本不关心您的缩进,它只遵循java语言的规则。

多个嵌套的条件语句,如if() else if() if(),很难确定(由人类)如何执行它们。

所以: 从不 使用多个没有花括号的语句。 (有些人甚至说要总是使用花括号,即使你只有一个陈述。)

当存在最微小的歧义时(如您编写的代码中所示),请确保始终添加花括号以确保编译器按照您的意图编译代码。

然后,您的代码将(可能)起作用。

答案 1 :(得分:0)

编码中的两件大事就是让您的代码易于阅读和实用。那些if语句不能满足这些需求。你也不要使用花括号......使用花括号{}!这是你的代码,但我把它组织得更干净一点。另外,老实说,我不确切地知道为什么它没有在您的原始代码中打印,但这不是问题。这个有效。

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

    }
}