【发布时间】:2021-09-16 17:31:15
【问题描述】:
我对我在这里做错了什么感到有点困惑。用户获得 3 卷,我正在尝试使用计数器来确定他们点击了 JavaFX 按钮的次数。当我在事件处理程序之前初始化 diceRollCounter 时,我得到一个错误。如果我在事件处理程序中初始化 diceRollCounter,每次单击按钮时,我都会将 diceRollCounter 重置为零,这违背了它的目的。
int diceRollCounter = 0;
rollDice.setOnAction(e-> {
if (diceRollCounter < 3) {
cup.shakeCup();
diceRollCounter++;
} else if (diceRollCounter > 3) {
Text noMoreRolls = new Text();
noMoreRolls.setText("You are out of rolls for this round");
noMoreRolls.setX(355);
noMoreRolls.setY(525);
root.getChildren().add(noMoreRolls);
}
});
【问题讨论】:
-
使
diceRollCounter成为类的成员而不是局部变量。
标签: java button javafx counter