我的if语句没有执行,我该如何解决?

时间:2015-02-24 08:37:37

标签: java xml user-interface javafx logic

我在JavaFX中制作BlackJack游戏。到目前为止,一切都很顺利。唯一缺少的是游戏应该停止,当牌的总价值超过21时玩家应该破产,就像真实游戏一样。我在代码中添加了一个if语句。好的新是,if语句执行。坏消息是,它在总值超过21之后执行。因此,当值为23时,玩家仍然可以点击。否则if语句将不会执行。它在玩家再次击中时执行,例如23变为25。

我希望我解释得很好,否则请给我反馈。

这是我的代码:

    public class FXMLDocumentController implements Initializable {

    private int playerValue, dealerValue, playerTotal, dealerTotal;





    Random ran = new Random();
    @FXML
    private TextArea dealerArea;
    @FXML
    private TextArea playerArea;
    @FXML
    private Label labelPlayerTotal;
    @FXML
    private Label labelDealerTotal;
    @FXML
    private Label labelWinner;
    @FXML
    private Button hitButton;
    @FXML
    private Button stayButton;
    @FXML
    private Button replayButton;





    public void drawCardForPlayer(ActionEvent event) {

        stayButton.setDisable(false);
        playerValue = ran.nextInt(14);

        while (playerValue <=1) {
            playerValue = ran.nextInt(14);
        }
        ######################################
        // I created this broken if statement
        if (playerTotal >21) {

            labelWinner.setText("You busted, dealer won!");
            hitButton.setDisable(true);
            stayButton.setDisable(true);

        }
        #########################
        switch (playerValue) {

        case 2:
            playerArea.appendText("\t" + Deck.drawTwo());
            playerTotal = 2 + playerTotal;
            labelPlayerTotal.setText("Playertotal:" + playerTotal);
            break;

        case 3:
            playerArea.appendText("\t" + Deck.drawThree()); 
            playerTotal = 3 + playerTotal;
            labelPlayerTotal.setText("Playertotal:" + playerTotal);
            break;

        case 4:
            playerArea.appendText("\t" + Deck.drawFour()); 
            playerTotal = 4 + playerTotal;
            labelPlayerTotal.setText("Playertotal:" + playerTotal);
            break;

        case 5:
            playerArea.appendText("\t" + Deck.drawFive()); 
            playerTotal = 5 + playerTotal;
            labelPlayerTotal.setText("Playertotal:" + playerTotal);
            break;

        case 6:
            playerArea.appendText("\t" + Deck.drawSix()); 
            playerTotal = 6 + playerTotal;
            labelPlayerTotal.setText("Playertotal:" + playerTotal);
            break;

        case 7:
            playerArea.appendText("\t" + Deck.drawSeven());
            playerTotal = 7 + playerTotal;
            labelPlayerTotal.setText("Playertotal:" + playerTotal);
            break;

        case 8:
            playerArea.appendText("\t" + Deck.drawEight()); 
            playerTotal = 8 + playerTotal;
            labelPlayerTotal.setText("Playertotal:" + playerTotal);
            break;

        case 9:
            playerArea.appendText("\t" + Deck.drawNine());
            playerTotal = 9 + playerTotal;
            labelPlayerTotal.setText("Playertotal:" + playerTotal);
            break;

        case 11:
            playerArea.appendText("\t" + Deck.drawJack());
            playerTotal = 10 + playerTotal;
            labelPlayerTotal.setText("Playertotal:" + playerTotal);
            break;
        case 12:
            playerArea.appendText("\t" + Deck.drawQueen()); 
            playerTotal = 10 + playerTotal;
            labelPlayerTotal.setText("Playertotal:" + playerTotal);
            break;
        case 13:
            playerArea.appendText("\t" + Deck.drawKing());
            playerTotal = 10 + playerTotal;
            labelPlayerTotal.setText("Playertotal:" + playerTotal);
            break;
        case 14:
            playerArea.appendText("\t" + Deck.drawAce());
            playerTotal = 11 + playerTotal;
            labelPlayerTotal.setText("Playertotal:" + playerTotal);
            break;

            default:
            break;

        }



    }
   }
   }

我调试了它并且意识到playerTotal的值最初是0,而playerTotal的值仅在switch-case中初始化,这导致了问题。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

playerTotalswitch之前未更新。对if

进行PlayerTotal + playerValue > 21语句检查

答案 1 :(得分:0)

您应该将if条件移到获取新随机卡的代码之前。

    if (playerTotal >21) {

        labelWinner.setText("You busted, dealer won!");
        hitButton.setDisable(true);
        stayButton.setDisable(true);

    }

    stayButton.setDisable(false);
    playerValue = ran.nextInt(14);

    while (playerValue <=1) {
        playerValue = ran.nextInt(14);
    }
    switch (playerValue) {
        ....