gpt4 book ai didi

Java编程: I get a issue when I enter a number and check if the number is correct

转载 作者:行者123 更新时间:2023-11-30 08:01:56 25 4
gpt4 key购买 nike

我创建了一个丹麦游戏,您应该猜测 1 到 10 之间的数字。

当我输入一个数字来检查我的猜测是否正确时,我在控制台中收到错误消息。

您知道为什么在输入数字并检查数字是否正确时遇到问题吗?

Game.java

import java.util.Random;

public class Game {
int randomNumber;

public Game() {
// Set randomNumber to randon number [1;10]
Random rand = new Random();
randomNumber = rand.nextInt(10) + 1;
}

public int getRandomNumber() {
return randomNumber;
}

}

GameFrame.java

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class GameFrame extends JFrame implements ActionListener{
private Game game;
private Player player;
private JTextField inputField;
private JLabel gameAnswer, tries;

public GameFrame(String title) {
super(title);
getContentPane().setLayout(new FlowLayout());
setSize(300, 100);

// Start game & add player
game = new Game();
player = new Player();

addComponents();

setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);

}

private void addComponents() {
JLabel tries = new JLabel("Forsøg: " + player.getTries());
getContentPane().add(tries);

inputField = new JTextField(8);
getContentPane().add(inputField);

JButton submitGuess = new JButton("Gæt tal");
submitGuess.addActionListener(this);
getContentPane().add(submitGuess);

JLabel gameAnswer = new JLabel("Gæt et tal mellem 1-10");
getContentPane().add(gameAnswer);
}

private void resetGame() {
// Create new randon number & new player
game = new Game();
player = new Player();

// Set default text in components
gameAnswer.setText("Gæt et tal mellem 1-10");
tries.setText("Forsøg: " + player.getTries());
}

@Override
public void actionPerformed(ActionEvent e) {
// Increment tries guessed
player.addTry();
tries.setText("Forsøg: " + player.getTries());


// Extract number guessed
String textEntered = inputField.getText();
int numberEntered = Integer.parseInt(textEntered);

// How was it compared to randomNumber?
if (numberEntered == game.getRandomNumber()) {
// CORRECT! - display optionpane
JOptionPane.showMessageDialog(null, "TILLYKKE! Du gættede rigtigt! Du brugte: " + player.getTries() + " forsøg.");

// Reset Game
resetGame();

} else if (numberEntered > game.getRandomNumber())
// To high
gameAnswer.setText("Forket! Du gættede for højt!");
else if (numberEntered < game.getRandomNumber())
// To low
gameAnswer.setText("Forkert! Du gættede for lavt!");

// Calculate result & display
}

}

Main.java

public class Main {

public static void main(String[] args) {
new GameFrame("Gæt et tal");
}
}

Player.java

public class Player {
private int tries;

public Player() {
tries = 0;
}

public void addTry() {
tries++;
}

public int getTries() {
return tries;
}
}

最佳答案

您将 private JLabel gameAnswer, attempts; 声明为类字段,但从未初始化它,因为在 addComponents() 方法中,您使用以下方式声明和初始化新对象:

JLabel tries = new JLabel("Forsøg: " + player.getTries());
JLabel gameAnswer = new JLabel("Gæt et tal mellem 1-10");

所以你会得到NullPointerException。您需要像这样声明它:

tries = new JLabel("Forsøg: " + player.getTries());
gameAnswer = new JLabel("Gæt et tal mellem 1-10");

它应该可以工作。

关于Java编程: I get a issue when I enter a number and check if the number is correct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31783550/

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