Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何制作一个数字猜谜游戏_Java_User Interface_Event Handling - Fatal编程技术网

Java 如何制作一个数字猜谜游戏

Java 如何制作一个数字猜谜游戏,java,user-interface,event-handling,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 {

我需要做一个猜谜游戏程序。我想不出任何方法来成功地记录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
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
导入java.util.Random;
公共类猜测编号扩展JFrame
{
Random randNum=新的Random();
int numToGuess=randNum.nextInt(1000);
int猜测1;
int猜测2;
公共静态void main(字符串[]args)
{
新猜测编号();
}//末端总管
公众猜测编号()
{
setTitle(“猜数字”);
setLayout(新的FlowLayout());
JLabel promptLbl1=新JLabel(“我有一个介于1和1000之间的数字,你能猜出我的数字吗?”);
添加(立即添加1);
JLabel promptLbl2=新JLabel(“请输入您的猜测”);
添加(立即添加2);
JTextField txtFld=新的JTextField(4);
添加(txtFld);
JLabel Lbl3=新的JLabel();
添加(Lbl3);
MyHandler=新的MyHandler();
txtFld.addActionListener(处理程序);
设置大小(300300);
setVisible(真);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}//结束构造函数
私有类MyHandler实现ActionListener
{
已执行的公共无效操作(操作事件ev)
{
字符串gs1;
猜测1=txtFld.getText();
guess1=整数.parseInt(gs1);
字符串gs2;
猜测2=txtFld.getText();
guess2=整数.parseInt(gs2);
如果(gs1>gs2)
txtFld.退根地面(颜色:蓝色);
否则如果(gs1numToGuess)
Lbl3(“过高”);
否则如果(gs2
关于空字段的问题是因为您在构造函数中声明了字段,而不是在类主体中声明了字段。只需将它们添加到其他声明的字段下面,就可以了:

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;
我还发现了另一个问题:

您不能将
字符串
=
进行比较,您需要使用
.equals
,如下所示:

if (gs2.equals(numToGuess))
那么它应该会起作用

编辑

哎呀,我刚刚注意到
numToGuess
是一个
int
。那样的话,你应该写信

if (Integer.parseInt(gs2) == numToGuess)
或相当于:

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

由于您要将
gs2
numToGuess
进行比较,因此它们需要具有相同的类型。因此,要么使用
Integer.parseInt()
gs2
更改为
Integer
,要么使用
String.valueOf()
将标签和文本字段的声明从构造函数移动到猜测数字

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);
    //...

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

MyHandler需要一个GuessTheNumber实例来访问
txtFld
Lbl3
。请注意代码的清晰度,这种缩进和命名会给您带来麻烦。请格式化您的代码。