骰子JAVAFX的最后一个值
我通常不去这样的网站,但我有一个严重的问题,无法真正找到如何解决这个问题 所以我有一个简单的骰子,当你按下一个按钮时,它就会滚动。它在控制台中打印出所有的骰子号码。现在我想保存最后一个int,这样我就可以用它进行计算了。现在的问题是,我真的不知道如何获取骰子的最后一个整数并在控制台中打印出来。有没有人有这方面的经验,可以帮助我?这是我的密码:骰子JAVAFX的最后一个值,java,javafx,Java,Javafx,我通常不去这样的网站,但我有一个严重的问题,无法真正找到如何解决这个问题 所以我有一个简单的骰子,当你按下一个按钮时,它就会滚动。它在控制台中打印出所有的骰子号码。现在我想保存最后一个int,这样我就可以用它进行计算了。现在的问题是,我真的不知道如何获取骰子的最后一个整数并在控制台中打印出来。有没有人有这方面的经验,可以帮助我?这是我的密码: btn.setText("Roll Die"); btn.setOnAction((ActionEvent event) -> {
btn.setText("Roll Die");
btn.setOnAction((ActionEvent event) -> {
btn.setDisable(true);//Disable Button
Random random = new Random();
int gekozen = Integer.parseInt(tf3.getText());
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(.3), (actionEvent) -> {
int tempRandom = random.nextInt(6) + 1;
System.out.println(tempRandom);
die.setDieFace(tempRandom);
}));
timeline.setCycleCount(random.nextInt(20) + 1);
timeline.play();
timeline.setOnFinished(actionEvent -> {
btn.setDisable(false);//Enable Button
});
});
最好的选择是将值存储在某个地方。您的
Die
类(或任何Die
类的来源)似乎是一个很好的候选。在一个设计良好的类中,如果将一个值传递给set方法,比如setXyz(x)
,那么调用getXyz()
,同时不对同一对象执行任何操作,将返回相同的值x
。因此,如果您已经按照标准Java模式设计了类,那么您可以这样做
timeline.setCycleCount(random.nextInt(20) + 1);
timeline.play();
timeline.setOnFinished(actionEvent -> {
btn.setDisable(false);//Enable Button
int dieValue = die.getDieFace();
System.out.println(dieValue);
});
另一个选项是提前计算所有临时模具值:
btn.setText("Roll Die");
btn.setOnAction((ActionEvent event) -> {
btn.setDisable(true);//Disable Button
Random random = new Random();
int gekozen = Integer.parseInt(tf3.getText());
int numTempValues = random.nextInt(20) + 1 ;
int[] tempValues = random.ints(1, 7).limit(numTempValues).toArray();
int finalDieValue = tempValues[numTempValues - 1];
Timeline timeline = new Timeline() ;
for (int i = 0 ; i < tempValues.length ; i++) {
KeyFrame keyFrame = new KeyFrame(Duration.seconds(0.3).multiply(i+1), actionEvent -> {
System.out.println(tempValues[i]);
die.setDieFace(tempValues[i]);
});
timeline.getKeyFrames().add(keyFrame);
}
timeline.setOnFinished(actionEvent -> {
btn.setDisable(false);//Enable Button
System.out.println("You rolled: "+finalDieValue);
});
timeline.play();
});
btn.setText(“辊模”);
btn.setOnAction((ActionEvent事件)->{
btn.setDisable(true);//禁用按钮
随机=新随机();
int-gekozen=Integer.parseInt(tf3.getText());
int numTempValues=random.nextInt(20)+1;
int[]tempValues=random.int(1,7).limit(numTempValues.toArray();
int finalDieValue=tempValues[numTempValues-1];
时间线=新时间线();
对于(int i=0;i{
System.out.println(tempValues[i]);
模具表面(温度值[i]);
});
timeline.getKeyFrames().add(keyFrame);
}
timeline.setOnFinished(actionEvent->{
btn.setDisable(false);//启用按钮
System.out.println(“您滚动:+finalDieValue”);
});
timeline.play();
});
您可以将int
变量添加到Die
类中
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
/**
*
* @author blj0011
*/
public class Die
{
ImageView dieFace;
Image[] images;
int dieValue;
public Die(Image[] images)
{
this.images = images;
dieFace = new ImageView(this.images[0]);//set default to image 0
dieValue = 1;
}
public Die(Image[] images, int dieFaceValue)
{
//Need to catch for values less than 1 and greater than 6!
this.images = images;
dieFace = new ImageView(this.images[dieFaceValue - 1]);
dieValue = dieFaceValue;
}
public ImageView getdieFace()
{
return dieFace;
}
public int getDieValue()
{
return dieValue;
}
public void setDieFace(int dieFaceValue)
{
//Need to catch for values less than 1 and greater than 6!
dieFace.setImage(this.images[dieFaceValue - 1]);
dieValue = dieFaceValue;
}
}
你就不能在
onFinished
处理程序中调用die.getDieFace()
吗?@James\u D不,因为这只是给了我一个不应该的图像。您正在将一个int
传递给setDieFace(…)
,因此如果调用getDieFace()
,如果Die
类定义正确,则应该返回一个int
。如果需要,您可以随时向其添加另一个返回int
值的方法。听起来您需要更正任何类die
的设计。编辑问题,不要在注释中放置代码。提供一个。具体来说,提供模具类代码和任何其他最小代码,以便有人可以复制和粘贴您的代码来复制问题。