我目前只停留在一个简单的程序上,该程序有一个猜测和重置按钮,用于猜测1-100的数字。用户获得5回合。我目前的问题是我无法正确运行重置按钮。按下它时,不是完全生成新游戏,而是仅生成一个新数字,但是当按下猜测时,再次使用生成的原始数字。
我会在其中包含一些代码片段,我觉得自己犯了很大的逻辑错误。
所以只需我的计数器和初始随机数变量,然后是猜测和重置按钮
query = '''
UPDATE Exercises
SET Exercise = ?, Weight = ?
WHERE ID = ?
'''
cur.execute(query, (exercise, weight, id_))
我的随机数生成器。
int counter = 0;
int num = genRan();
/////////////////////////////////////
guessButton.setOnAction(e -> {
int numToGuess = num;
gameSetUp(guessField, counter, numToGuess);
});
//////////////////////////////////////////////////////////
resetButton.setOnAction(e -> {
System.out.println("Generating new game...");
int newNum = genRan();
int newCounter = 0;
gameSetUp(guessField, newCounter, newNum);
});
我认为这一切都出了问题......
public int genRan() {
Random rand = new Random();
int numToGuess = rand.nextInt(100) + 1;
System.out.println("number is" + numToGuess);
return numToGuess;
}
感谢任何帮助,谢谢。
答案 0 :(得分:0)
以下是一个示例应用,Initial State
和Game Play State
分开。
import java.util.Random;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* @author Sedrick
*/
public class JavaFXApplication47 extends Application {
TextField guessField = new TextField();
int counter = 0;
int numberToGuess = -1;
@Override
public void start(Stage primaryStage) {
guessField.setPromptText("Press New Game Button!");
Button btnNewGame = new Button("New Game");
btnNewGame.setOnAction(actionEvent ->{
gameSetup();
});
Button btnGuess = new Button("Guess");
btnGuess.setOnAction(actionEvent -> {
gamePlay();
});
VBox root = new VBox();
root.getChildren().addAll(guessField, new HBox(btnNewGame, btnGuess));
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
btnNewGame.requestFocus();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
public void gameSetup()
{
guessField.setPromptText("Enter your guess");//set guessField to zero
counter = 0; //set counter to zero
//set numberToGuess to a new random number
Random random = new Random();
numberToGuess = random.nextInt(100) + 1;
System.out.println("Number to guess: " + numberToGuess);
}
public void gamePlay()
{
int guess = Integer.parseInt(guessField.getText());
if(counter == 5)//Check it counter == 5
{
System.out.println("Game over! You lose!");
guessField.setPromptText("Game over! You lose!");
guessField.setText("");
}
else{
if(guess == numberToGuess)
{
System.out.println("You win");
guessField.setPromptText("Your guess of " + guess + " is correct! You win!");
guessField.setText("");
}
else if(guess < numberToGuess)
{
System.out.println("You guessed to low");
guessField.setPromptText("You guessed to low! Try again.");
guessField.setText("");
counter++;
}
else
{
System.out.println("You guessed to high! Try again.");
guessField.setPromptText("You guessed to high! Try again.");
guessField.setText("");
counter++;
}
}
}
}