提问者:小点点

编程分配数字游戏[关闭]


编辑问题以包括所需的行为、特定的问题或错误以及重现问题所需的最短代码。这将有助于其他人回答问题。

代码有效,但当我移动滑块时,它不会改变难度。

这是代码,请帮助我今天需要它:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.Random;

public class numgame extends Application {
private int answer;
private Label levelLabel;
private Slider difficultySlider;
private Label resultLabel;
private TextField guessField;

public void start(Stage stage) {
levelLabel = new Label("Easy");
difficultySlider = new Slider(1, 3, 0);
difficultySlider.setOrientation(Orientation.VERTICAL);
difficultySlider.setShowTickLabels(true);
difficultySlider.setShowTickMarks(true);
difficultySlider.setMajorTickUnit(1);
difficultySlider.setBlockIncrement(1);
resultLabel = new Label();
guessField = new TextField();
Button guessButton = new Button("Guess");
guessButton.setOnAction(e -> guessNumber());
GridPane gridPane = new GridPane();
gridPane.setAlignment(Pos.CENTER);
gridPane.setHgap(10);
gridPane.setVgap(10);
gridPane.setPadding(new Insets(25, 25, 25, 25));
gridPane.add(new Label("Guess the number:"), 0, 0);
gridPane.add(guessField, 1, 0);
gridPane.add(guessButton, 2, 0);
gridPane.add(resultLabel, 1, 1);
HBox sliderBox = new HBox();
sliderBox.setAlignment(Pos.CENTER);
sliderBox.getChildren().addAll(new Label("Difficulty Level: "), levelLabel,      difficultySlider);
VBox vbox = new VBox();
vbox.setAlignment(Pos.CENTER);
vbox.getChildren().addAll(sliderBox, gridPane);

Scene scene = new Scene(vbox, 400, 200);
stage.setTitle("Number Guessing Game");
stage.setScene(scene);
stage.show();

setLevel(difficultySlider.getValue());
}

private void guessNumber() {
try {
int guess = Integer.parseInt(guessField.getText());
if (guess == answer) {
resultLabel.setText("You guessed correctly!");
} else {
resultLabel.setText("Sorry, the correct answer is " + answer);
}
} catch (NumberFormatException e) {
resultLabel.setText("Invalid input. Please enter an integer.");
}
}

private void setLevel(double level) {
int max = 0;
switch ((int) level) {
case 1:
levelLabel.setText("Easy");
max = 10;
break;
case 2:
levelLabel.setText("Medium");
max = 50;
break;
case 3:
levelLabel.setText("Hard");
max = 100;
break;
}
answer = new Random().nextInt(max) + 1;
}

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

我今天期末考试需要这个作业,但是我不知道如何解决它,我想尽了一切办法,所以请帮帮我

额外:fdyhtyestg erg sae rg erg e h e hert h a e yh aeth a r hgert hbt hy et h t h e h e h e e he s ht s h 5t ser 6u t hs er6t us r u te rs u 65u t h s g t h hvj jt cv yfc uycuytcuyc fc ytfytk hvkcj cfkj hgck gcfktg fcktgj kutfc kgcv fktc td ktg, c ktcd tkgc tc gck tck cd yfck tct kj cjfh cfiy cg vftufcvikytc ytcytciytc ytckitckutc kuytckutck tuckckt ckckct kuctku gtckyxcykjrxsj rfykxrxkc


共1个答案

匿名用户

尝试添加一个监听器到困难滑块更新水平和回答当滑块值改变。

difficultySlider.valueProperty().addListener((observable, oldValue, newValue) -> {
    setLevel(newValue.intValue());
});

这应该在每次滑块值更改时使用新的滑块值调用setLevel方法。