Java 满足条件时未触发的if语句

Java 满足条件时未触发的if语句,java,javafx,Java,Javafx,我正在构建一个简单的21人游戏。一切正常,但当我点击我分配给“Stand”功能的按钮时,没有一个if语句阻止触发事件,尽管我满足其中一个条件,这取决于已经发了哪些牌。我已经测试了所有不同的陈述,我想有一些洞察力,或者第二双眼睛看到一些我没有看到的东西 我已经多次测试该函数,并多次重新编写它。我已经测试了这个函数,但是它仍然没有触发 这就是所讨论的功能: //when player hits stand button public void Stand(TextField playerNum, T

我正在构建一个简单的21人游戏。一切正常,但当我点击我分配给“Stand”功能的按钮时,没有一个if语句阻止触发事件,尽管我满足其中一个条件,这取决于已经发了哪些牌。我已经测试了所有不同的陈述,我想有一些洞察力,或者第二双眼睛看到一些我没有看到的东西

我已经多次测试该函数,并多次重新编写它。我已经测试了这个函数,但是它仍然没有触发

这就是所讨论的功能:

//when player hits stand button
public void Stand(TextField playerNum, TextField dealerNum, TilePane b, Button hit, Button stand, Button deal, TextField handsLostNum, TextField handsWonNum) {
    //obtain current final scores when player stands
    playerFinal = Integer.parseInt(playerNum.getText());
    dealerFinal = Integer.parseInt(dealerNum.getText());

    if (playerFinal > dealerFinal) {
        hit.setVisible(false);
        stand.setVisible(false);
        deal.setVisible(true);

        playerNum.setText("YOU WIN!");
        dealerNum.setText("YOU WIN!");
        handsWon += 1;
        String temp = Integer.toString(handsWon);
        handsWonNum.setText(temp);
    }

    if (dealerFinal > playerFinal) {
        hit.setVisible(false);
        stand.setVisible(false);
        deal.setVisible(true);

        playerNum.setText("YOU LOSE!");
        dealerNum.setText("YOU LOSE!");
        handsLost += 1;
        String temp = Integer.toString(handsLost);
        handsLostNum.setText(temp);
    }

    if (dealerFinal == playerFinal) {
        playerNum.setText("DRAW! PLAY AGAIN!");
        dealerNum.setText("DRAW! PLAY AGAIN!");
        hit.setVisible(false);
        stand.setVisible(false);
        deal.setVisible(true);
    }
    handsWon = 0;
    handsLost = 0;
} //END STAND METHOD
有助于满足这一要求的条件如下:

//method to add scores to text fields
public void addScores(int pScore, int dScore, TextField playerNum, TextField dealerNum) {
    //ADD PLAYER SCORE
    String playerScore = playerNum.getText();
    int playerCurrent = Integer.parseInt(playerScore);
    int newCurrent = playerCurrent + dScore;
    String newScore = Integer.toString(newCurrent);
    playerNum.setText(newScore);

    //ADD DEALER SCORE
    String dealerScore = dealerNum.getText();
    int dealerCurrent = Integer.parseInt(dealerScore);
    int newDealCurrent = dealerCurrent + pScore;
    String newDealScore = Integer.toString(newDealCurrent);
    dealerNum.setText(newDealScore);
}
我将分数添加到文本字段中,然后稍后在项目中再次提取它们。然而,即使这些值满足大于对手值的条件,该语句也不会触发


预期的结果是,当我单击“Stand”按钮时,该语句被触发,然后添加到总计数中的变量被激活。

尝试在每一步中放入System.out,以检查它是否真的达到了目标。将一个作为Stand方法中的第一个语句,如:System.out.println(“in Stand方法”)

然后,在if语句之前和语句内部添加更多内容,如:

System.out.format("Before: playerFinal : %s, dealerFinal: %s, playerFinal > dealerFinal: %d %n", playerFinal, dealerFinal, playerFinal > dealerFinal);
if (playerFinal > dealerFinal) {
System.out.format("In: playerFinal : %s, dealerFinal: %s, playerFinal > dealerFinal: %d %n", playerFinal, dealerFinal, playerFinal > dealerFinal);
对每个方法都执行此操作,以查看该方法是否正在实际运行以及值是什么

如果您看到If语句正在执行,并且流程正在进入其中,但没有看到GUI元素上的任何更改,那么请尝试使用:

Platform.runLater(() -> {
 // Your GUI changes code
  playerNum.setText("YOU WIN!");
  dealerNum.setText("YOU WIN!");
});
Platform.runLater接收一个runnable作为参数,如果使用JavaFX,这是更新GUI的正确方法


有时,您可能会保存文件并运行它,而IDE实际上不会编译它,而是运行旧代码。在这种情况下,您可以尝试重新启动IDE,然后再试一次。

您确定您的输入是您认为的吗?如果执行了
块,为什么您认为没有执行任何
?首先要做的是在设置dealerFinal和playerFinal后使用调试器或添加
printlin()
语句,以确保这些字段包含您期望的内容。然后添加
System.out.println(“玩家赢/经销商赢/推”)代码,查看控制台中是否有调试输出。您的UI可能无法正确反映程序状态。确保正确地解决了实际问题,这样就不会有人浪费时间去做错误的事情。是否确定单击按钮
时正确分配了处理程序?可能根本没有调用您的单击处理程序。设置断点和使用IDE调试程序会更清晰、更有用。请参阅您发布的ar not事件处理程序方法。我们基本上不知道这些方法是否以及如何被调用。问题需要提供答案。在这种情况下,对这两个方法的一系列调用可以将代码的大小保持在最小,但我猜这不是错误所在。。。