Date 同一个号码?

Date 同一个号码?,date,button,random,time,Date,Button,Random,Time,我最近开始使用JavaFX,我正在为学校编写一个程序,但遇到了一个小问题。整个项目已经完成了,除了一个我无法理解的bug和一个我关心的问题。目标是有一个小框,用于检查加法问题是否正确回答,并可以提供新问题 我有一个错误,我想问题出在我的新按钮处理程序中,但找不到。在最初的问题之后,数字开始相同。如中所示,它可能开始:7+2 但结果将是:1+1、2+2、3+3等等 我的另一个问题是:如果有两个按钮,那么这需要多个处理程序,还是有一种方法可以将其放入一个处理程序中?我还没有在这种环境下正式学习Lam

我最近开始使用JavaFX,我正在为学校编写一个程序,但遇到了一个小问题。整个项目已经完成了,除了一个我无法理解的bug和一个我关心的问题。目标是有一个小框,用于检查加法问题是否正确回答,并可以提供新问题

我有一个错误,我想问题出在我的新按钮处理程序中,但找不到。在最初的问题之后,数字开始相同。如中所示,它可能开始:7+2 但结果将是:1+1、2+2、3+3等等

我的另一个问题是:如果有两个按钮,那么这需要多个处理程序,还是有一种方法可以将其放入一个处理程序中?我还没有在这种环境下正式学习Lambda

代码如下:

package application;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import java.util.Random;

import java.util.Date;

public class MathDrill extends Application
{
    private Button checkButton, newButton;
    private Label topNumLabel, botNumLabel, reply;
    private TextField input;


    //Check
    class BCHCheck implements EventHandler<ActionEvent>
    {
        @Override
        public void handle(ActionEvent event)
        {
            Integer answer;

            try
            {
                answer = Integer.parseInt(input.getText());

                Integer topNum = Integer.parseInt(topNumLabel.getText()),
                        botNum = Integer.parseInt(botNumLabel.getText());

                if(topNum + botNum == answer)
                {
                    reply.setText("Correct!");
                }
                else
                {
                    reply.setText("Incorrect!");
                }
            }
            catch(NumberFormatException e)
            {
                reply.setText("Try again with an integer please.");
            }               
        }
    }

    //New
    class BCHNew implements EventHandler<ActionEvent>
    {
        @Override
        public void handle(ActionEvent event)
        {
            topNumLabel.setText(Integer.toString(rngInt()));

            botNumLabel.setText(Integer.toString(rngInt()));

            reply.setText("");
        }
    }

    public static void main(String[] argv)
    {
        launch(argv);
    }

    public void start(Stage stage)
    {
        topNumLabel = new Label(Integer.toString(rngInt()));
        botNumLabel = new Label(Integer.toString(rngInt()));
        reply = new Label("");

        Label words = new Label("Solve this:"),
               plus = new Label("+"),
           equalBar = new Label("-----");

        input = new TextField();


        GridPane problem = new GridPane();
        problem.add(words, 0, 0);
        problem.add(plus, 0, 4);

        problem.add(topNumLabel,1, 1);
        problem.add(botNumLabel, 1, 2);
        problem.add(equalBar, 1, 3);
        problem.add(input, 1, 4);
        problem.add(reply, 1, 5);

        checkButton = new Button("Check");
        checkButton.setOnAction(new BCHCheck());

        checkButton.setMinSize(100, 100);

        newButton = new Button("New");
        newButton.setOnAction(new BCHNew());

        newButton.setMinSize(100, 100);

        GridPane buttons = new GridPane();
        buttons.add(checkButton, 0, 0);
        buttons.add(newButton, 0, 1);


        BorderPane bp = new BorderPane();

        bp.setLeft(buttons);
        bp.setCenter(problem);

        Scene scene = new Scene(bp);



        stage.setTitle("Math Drills");
        stage.setScene(scene);
        stage.setMinWidth(350);
        stage.show();
    }

    /**
     * Returns rng int value from 0-9
     * 
     * @return  rng int, [0, 9]
     */
    private int rngInt()
    {
         return new Random(new Date().getTime()).nextInt(10);
    }
}

谢谢您的帮助。

您的rngInt函数每次调用时都会生成一个新的随机对象。由于您总是使用new Date.getTime为其种子,并且该函数只有毫秒精度,因此,如果您在同一毫秒内调用它两次,这对计算机来说是一段很长的时间,那么您将得到相同的数字序列,或者在您的情况下,两次都只得到一个数字。要修复此问题,只需创建一个随机对象,然后对其重复下一次调用,而不是为每次调用创建一个新的随机对象。

为什么要为所需的每个随机数创建一个新的随机对象?它们不是一次性使用的。别这样。谢谢你,这打破了我的一个坏习惯。只是好奇,为什么这能纠正我的错误?我不太明白为什么。