簡體   English   中英

嵌套if-else語句有麻煩

[英]Trouble with nested if - else statements

我對編碼非常陌生,並嘗試根據我在這里看到的代碼制作剪刀石頭布的代碼。 但是,當系統在打印計算機播放的內容后輸出游戲結果時,它只是不打印。 有任何想法嗎? 謝謝!

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

if語句的嵌套是完全錯誤的。 縮進(差不多)暗示了您希望代碼如何執行,但是編譯器完全不關心縮進,它僅遵循Java語言的規則。

眾所周知,多個嵌套條件語句(如if() else if() if()很難(由人決定)如何執行。

因此:如果沒有大括號,請不要使用多個語句。 (甚至有人說總是使用花括號,即使您只有一條語句。)

當甚至有一點點歧義時(如您編寫的代碼中所述),請確保始終添加大括號以確保編譯器將按您希望的方式編譯您的代碼。

然后,您的代碼將(可能)正常工作。

編碼的兩件大事使您的代碼既易於閱讀又可以正常工作。 這些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.");
        }

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM