gpt4 book ai didi

java - 为什么当我改变场景时随机边界的值会重置?

转载 作者:行者123 更新时间:2023-12-01 18:36:50 38 4
gpt4 key购买 nike

我被一个错误困扰了很长一段时间。我在 JavaFX 中有两个类( Controller 和猜谜游戏)。在 Controller 中,我在“设置”菜单的场景 3 中有一个 slider 。当用户移动 slider 时,我获取 slider 的值,并将该值设置为随机数的范围。但是,当我切换回菜单,然后切换到场景 2 时,绑定(bind)的值又回到起始值 100。问题一定是场景 1 到 2 之间的问题。有谁知道如果我更改了值,为什么该值会重置回 100它与 slider 到另一个值?谢谢

Controller :

public class Controller {

GuessingGame guessingGame = new GuessingGame();;

@FXML
private TextField textfield;
@FXML
private Label countText;
public Label notification;
public Label textBetween;
@FXML
public Slider sliderSettings;

private int currentNumber;

public void clickedOnExit() {
System.exit(1);
}

;

public void clickedOnSettings() {
Main.pStage.setScene(Main.scene3);
}

;

public void clickedOnPlay() {
Main.pStage.setScene(Main.scene2); //Beim wechsel zu szene 2 wird der wert wieder auf 100 gesetzt
System.out.println(guessingGame.getRandomBound());
}

;

public void clickedBackToMenu() {
Main.pStage.setScene(Main.scene1);
}

;

public void clickedBackToMenuSettings() {
Main.pStage.setScene(Main.scene1);
}


//SLIDER
public void sliderSetValue() {
guessingGame.setRandomBound((int) sliderSettings.getValue());
System.out.println(guessingGame.getRandomBound());
}

;

public void enteredTextfield() {

currentNumber = Integer.parseInt(textfield.getText());

GuessingGame.Result checkNumber;

//Setzt
checkNumber = guessingGame.evaluateEnteredNumber(currentNumber);

if (checkNumber == GuessingGame.Result.HIGHER) {
notification.setText("HIGHER");
}
;
if (checkNumber == GuessingGame.Result.LOWER) {
notification.setText("LOWER");
}
;
if (checkNumber == GuessingGame.Result.EQUALS) {
notification.setText("YOU GOT IT!");
textBetween.setText("Congratulations! The number was: " + guessingGame.getNumber());
}
;
textfield.clear();
countText.setText("" + guessingGame.getCounter()); //Gibts ihr eine bessere Lösung?
}
}

猜谜游戏:

import java.util.Random;

public class GuessingGame {

Random rand = new Random();

private int number;
private int counter;
//Variable die maxhöhe der Zufallszahl am Anfang auf 100 Stand
private int randomBound = 100;

public enum Result {
EQUALS, HIGHER, LOWER
}

//Random number muss noch implementiert werden, sodass sie im Bound steht
public GuessingGame() {
getNumberToGuess(); //Konstruktor
}

public int getNumberToGuess() {
this.number = rand.nextInt(randomBound); //im bound muss der wert vom slide sein
return number;
}

//Methode für das Spielgeschehen gibt Das Ergebnis wieder obs höher/tiefer/gleich ist
public Result evaluateEnteredNumber(int enteredNumber) {
Result result;

counter++;

if (number > enteredNumber) {
result = Result.HIGHER;
} else if (number < enteredNumber) {
result = Result.LOWER;
} else {
result = Result.EQUALS;
}
return result;
}


//----------------------------------------------------//
//Getter und Setter//

//Setter und Getter für die Zufallszahl
public void setNumber(int newNumber) {
this.number = newNumber;
}
public int getNumber() {
return number;
}
//Setter und Getter für den Versuchzähler
public void setCounter(int newCounter) {
this.counter = newCounter;
}
public int getCounter() {
return counter;
}
//Setter und Getter für Die maxhöhe der Zufallszahl
public void setRandomBound(int newRandomBound) {
this.randomBound = newRandomBound;
}
public int getRandomBound() {
return randomBound;
}
}

场景1:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>


<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Button fx:id="exitButton" layoutX="505.0" layoutY="349.0" mnemonicParsing="false" onAction="#clickedOnExit" prefHeight="37.0" prefWidth="81.0" text="Exit" />
<Button fx:id="playButton" layoutX="237.0" layoutY="163.0" mnemonicParsing="false" onAction="#clickedOnPlay" prefHeight="37.0" prefWidth="126.0" text="Play" />
<Button fx:id="settingsButton" layoutX="237.0" layoutY="213.0" mnemonicParsing="false" onAction="#clickedOnSettings" prefHeight="37.0" prefWidth="126.0" text="Settings" />
<Label layoutX="66.0" layoutY="14.0" prefHeight="117.0" prefWidth="469.0" text="GUESSING GAME">
<font>
<Font name="Arial Black" size="49.0" />
</font>
</Label>
</children>
</AnchorPane>

场景2:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Label layoutX="72.0" prefHeight="20.0" prefWidth="457.0" text="GUESSING GAME">
<font>
<Font name="Arial Black" size="48.0" />
</font>
</Label>
<Label layoutX="72.0" layoutY="187.0" text="Your guess:">
<font>
<Font name="Arial" size="22.0" />
</font>
</Label>
<Label layoutX="395.0" layoutY="187.0" text="Attempts:">
<font>
<Font name="Arial" size="22.0" />
</font>
</Label>
<TextField fx:id="textfield" layoutX="197.0" layoutY="188.0" onAction="#enteredTextfield" prefHeight="25.0" prefWidth="66.0" />
<Label fx:id="countText" layoutX="496.0" layoutY="188.0" text="0">
<font>
<Font name="Arial" size="22.0" />
</font>
</Label>
<Label layoutX="48.0" layoutY="71.0" text="My number is a random number up to 100! Try to guess it!">
<font>
<Font name="Arial Black" size="16.0" />
</font>
</Label>
<Label fx:id="notification" layoutX="221.0" layoutY="120.0" text="Type a number!">
<font>
<Font name="Arial Black" size="22.0" />
</font>
</Label>
<Label fx:id="textBetween" layoutX="72.0" layoutY="272.0" prefHeight="26.0" prefWidth="356.0" text="Your number is between 0 and 100">
<font>
<Font name="Arial Black" size="17.0" />
</font>
</Label>
<Button fx:id="backToMenu" layoutX="485.0" layoutY="361.0" mnemonicParsing="false" onAction="#clickedBackToMenu" text="Back to Menu" />
</children>
</AnchorPane>

场景3:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Slider fx:id="sliderSettings" layoutY="200.0" max="1000.0" minorTickCount="4" onMouseReleased="#sliderSetValue" prefHeight="38.0" prefWidth="592.0" showTickLabels="true" showTickMarks="true" snapToTicks="true" value="100.0" />
<Label layoutX="4.0" layoutY="117.0" prefHeight="47.0" prefWidth="592.0" text="How High can the number you want to guess be?">
<font>
<Font name="Arial Black" size="22.0" />
</font>
</Label>
<Button fx:id="backToMenuSettings" layoutX="497.0" layoutY="361.0" mnemonicParsing="false" onAction="#clickedBackToMenuSettings" text="Back to Menu" />
</children>
</AnchorPane>

主要:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {
public static Stage pStage;

public static Parent root1;
public static Parent root2;
public static Parent root3;

public static Scene scene1;
public static Scene scene2;
public static Scene scene3;

public Main() {
}

@Override
public void start(Stage stage) throws Exception {

pStage = stage;

root1 = FXMLLoader.load(getClass().getResource("scene1.fxml"));
root2 = FXMLLoader.load(getClass().getResource("scene2.fxml"));
root3 = FXMLLoader.load(getClass().getResource("scene3.fxml"));


scene1 = new Scene(root1);
scene2 = new Scene(root2);
scene3 = new Scene(root3);

stage.setScene(scene1);
stage.show();

}

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

抱歉,节点是德语的。他们是为了让我弄清楚为什么会发生这种情况,但应该是在场景的变化中。感谢您的帮助!

最佳答案

问题只是,我在一开始就初始化了对象,并在每个 Controller 中初始化了一个新对象。所以我没有处理同样的问题,所以值(value)就丢失了。

关于java - 为什么当我改变场景时随机边界的值会重置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60018508/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com