如何制作数字猜谜游戏

时间:2013-11-30 23:30:03

标签: java user-interface event-handling

我需要制作一个猜谜游戏程序。我无法想出任何方法来成功记录2个猜测进行比较。我对“actionPerformed”方法遇到的问题最多。它没有链接到构造函数 - 就像txtFld和Lbl3一样,它说它是一个空指针。这是我到目前为止所做的:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;


public class GuessTheNumber extends JFrame
{
    Random randNum = new Random();
    int numToGuess = randNum.nextInt(1000);
    int guess1;
    int guess2;

    public static void main(String [] args)
    {
        new GuessTheNumber();

    } // end main

    public GuessTheNumber()
    {
        setTitle("Guess The Number");
        setLayout(new FlowLayout());

        JLabel promptLbl1 = new JLabel("I have a number between 1 and 1000.  Can you guess my number?");
        add(promptLbl1);
        JLabel promptLbl2 = new JLabel("Please enter your guess.");
        add(promptLbl2);
        JTextField txtFld = new JTextField(4);
        add(txtFld);
        JLabel Lbl3 = new JLabel();
        add(Lbl3);
        MyHandler handler = new MyHandler();
        txtFld.addActionListener(handler);
        setSize(300,300);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }  //end constructor

    private class MyHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent ev)
        {
            String gs1;
            guess1 = txtFld.getText();  
            guess1 = Integer.parseInt(gs1);
            String gs2;
            guess2 = txtFld.getText();  
            guess2 = Integer.parseInt(gs2);

            if (gs1 > gs2)
                txtFld.setBackground(color.blue);

            else if (gs1 < gs2)
                txtFld.setBackground(color.red);

            if (gs2 == numToGuess)  
            {
                Lbl3("Correct!"); 
                txtFld.setBackground(color.green);
            }

            else if (gs2 > numToGuess)
                Lbl3("Too High");

            else if (gs2 < numToGuess)
                Lbl3("Too Low");

        } // end actionPerformed  

    } // end MyHandler

} // end GuessTheNumber

2 个答案:

答案 0 :(得分:0)

您询问空字段的问题是因为您在构造函数而不是类主体中声明了字段。只需将它们添加到其他声明的字段下面,它应该可以工作:

public class GuessTheNumber extends JFrame
{
    Random randNum = new Random();
    int numToGuess = randNum.nextInt(1000);
    int guess1;
    int guess2;
    JLabel promptLbl1;
    JLabel promptLbl2;
    JTextField txtFld;
    JLabel Lbl3;

我发现了另一个问题:

您无法将String==进行比较,而是需要使用.equals,而不是这样:

if (gs2.equals(numToGuess))

然后它应该工作。

编辑

哎呀,我刚注意到numToGuessint。在这种情况下,你应该写

if (Integer.parseInt(gs2) == numToGuess)

或等效地:

if (gs2.equals(String.valueOf(numToGuess))

由于您要将gs2numToGuess进行比较,因此它们必须属于同一类型。因此,您可以使用gs2Integer更改为Integer.parseInt(),或使用numToGuessString更改为String.valueOf()

答案 1 :(得分:0)

您必须将标签和文本字段的声明从构造函数移动到GuessTheNumber。

public class GuessTheNumber extends JFrame
{
Random randNum = new Random();
int numToGuess = randNum.nextInt(1000);
int guess1;
int guess2;

JLabel promptLbl1;
JLabel promptLbl2;
JTextField txtFld;
JLabel Lbl3;
//...


public GuessTheNumber()
{
    setTitle("Guess The Number");
    setLayout(new FlowLayout());

    promptLbl1 = new JLabel("I have a number between 1 and 1000.  Can you guess my number?");
    add(promptLbl1);
    promptLbl2 = new JLabel("Please enter your guess.");
    add(promptLbl2);
    txtFld = new JTextField(4);
    add(txtFld);
    Lbl3 = new JLabel();
    add(Lbl3);
    //...

否则,您只能在构造函数中访问它们。