这就是我的简单程序。该计划缺乏的是其功能。 每当我点击提交。在控制台上必须有一个随机数,但是有一些错误,我无法转换 在TextField中输入随机生成的数字。
我遇到了一些错误。
这是我的代码
package sample;
import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene. Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.geometry.Insets;
import static java.lang.Math.*;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("Random Number Generator");
HBox layout = new HBox();
layout.setPadding(new Insets(25,12,15,20));
layout.setSpacing(10);
Scene scene = new Scene(layout, 400, 80);
primaryStage.setScene(scene);
TextField text = new TextField();
text.setMinWidth(200);
text.setMaxWidth(110);
Button button = new Button("Submit");
button.setOnAction(e -> RandGen(text, text.getText())); //Eventhandler
Label label = new Label();
label.setText("From 0 to :");
layout.getChildren().addAll(label, text, button);
primaryStage.show();
}
//start of functionalities
private void RandGen(TextField input, String Message){
try{
double Random = Math.random(input.getText());
System.out.println("The generated number from 0 to " + input.getText() +"is " + Random);
}catch(NumberFormatException e){
System.out.println("'" + Message + "' is not a number");
}
}
public static void main(String[] args) {
launch(args);
}
}
我只是想让我的程序输入文本字段并将其用作 Math.Random(input.getText())但有错误
但它给了我这些错误
答案 0 :(得分:1)
这是让你入门的东西。我没有改变你的代码太多,所以你可以理解。
<强>更新强>
package sample;
import javafx.application.Application;
import javafx.scene. Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.geometry.Insets;
import java.util.Random;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("Random Number Generator");
HBox layout = new HBox();
layout.setPadding(new Insets(25,12,15,20));
layout.setSpacing(10);
Scene scene = new Scene(layout, 400, 80);
primaryStage.setScene(scene);
TextField text = new TextField();
text.setMinWidth(200);
text.setMaxWidth(110);
Button button = new Button("Submit");
button.setOnAction(e -> {
RandGen(text, text.getText());
}); //Eventhandler
Label label = new Label();
label.setText("From 0 to :");
layout.getChildren().addAll(label, text, button);
primaryStage.show();
}
//start of functionalities
private void RandGen(TextField input, String Message){
Random rnd = new Random();
try{
double number = Double.parseDouble(Message);
double rndNum = 0 + number * rnd.nextDouble();
// input.setText(Double.toString(rndNum));
input.setText(Double.valueOf(rndNum).toString());
// System.out.println("The generated number from 0 to " + input.getText() +"is " + Random);
}catch(NumberFormatException e){
System.out.println("'" + Message + "' is not a number");
}
}
public static void main(String[] args) {
launch(args);
}
}
答案 1 :(得分:0)
你甚至没有使用&#34;输入&#34;。首先从&#34;输入&#34;创建双号。
double value = Double.parseDouble(input.getText());
然后像这样创建你的随机数
Random r = new Random();
int Low = 10;
int High = 100;
int Result = r.nextInt(High-Low) + Low;