首页 > 解决方案 > JavaFX 相同的号码?

问题描述

我最近开始使用 JavaFX,并且在我正在为学校工作的程序上遇到了一个小问题。整个项目是完整的,除了一个我无法弄清楚的错误和一个我担心的问题。目标是有一个小盒子来检查加法问题是否得到正确回答,并可以提供新问题。

我遇到的错误是这样的(我认为问题出在我的“新建”按钮的处理程序中,但找不到。):在最初的问题之后,数字开始相同。如,它可能开始: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);
    }
}

谢谢您的帮助。

标签: datebuttonrandomtime

解决方案


每次调用时,您的rngInt函数都会创建一个新对象。Random由于您总是使用 播种它new Date().getTime(),并且该函数只有毫秒精度,因此如果您在同一毫秒内调用它两次(这对计算机来说是很长的时间),您将获得相同的数字序列(或只是单个数字)你的情况)两次。要修复它,只创建一个Random对象,然后重复nextInt调用它,而不是Random为每个调用创建一个新对象。


推荐阅读