java 编程分配数游戏[已关闭]

mzmfm0qo  于 2023-05-12  发布在  Java
关注(0)|答案(1)|浏览(83)

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
2天前关闭。
Improve this question
代码工作,但当我移动滑块它不会改变难度.
下面是代码,请帮助我今天需要它:

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);
}
}

我需要这个作业今天的期末考试,但idk如何解决它,我尝试了一切我能想到的,所以请帮助我
额外:fdyhtyestg erg sae rg e h e hert h a e yh aeth a rgert hbt hy et h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h e h c ktcd tkgc tc gck tck cd yfck tct kj cjfh cfiy cg vftufcvikytc ytcytciytc ytckitckutc kuytckutck tuckckuckt ckckct kuctkctku gtckyxcykjrxsj rfykxrxkc

new9mtju

new9mtju1#

尝试向difficultySlider添加一个侦听器,以在滑块值更改时更新级别并回答。

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

每次滑块值更改时,都应使用新的滑块值调用setLevel方法。

相关问题