[英]How to make a number guessing game
I need to make a guessing game program. 我需要制作一个猜谜游戏程序。 I can't figure out any way to successfully log 2 guesses for comparison.
我找不到任何方法可以成功记录2个猜测以进行比较。 I am having the most problems with the "actionPerformed" method.
我在使用“ actionPerformed”方法时遇到最多的问题。 It's not linking to the constructor - like with txtFld and Lbl3, it says it's a null pointer.
它没有链接到构造函数-与txtFld和Lbl3一样,它表示它是一个空指针。 Here's what I have so far:
这是我到目前为止的内容:
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
The question you were asking about the null fields is because you declared your fields in your constructor instead of the class body. 您问有关null字段的问题是因为您在构造函数中而不是在类主体中声明了字段。 Just add them below the other declared fields and it should work:
只需将它们添加到其他声明的字段下面,它应该可以工作:
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;
I found another problem as well: 我也发现了另一个问题:
You cannot compare String
with ==
you need to use .equals
instead like so: 您不能将
String
与==
进行比较,而是需要使用.equals
来代替:
if (gs2.equals(numToGuess))
Then it should work. 然后它应该工作。
EDIT 编辑
Whoops, I just noticed that numToGuess
is an int
. 糟糕,我刚刚注意到
numToGuess
是一个int
。 In that case you should write 在这种情况下,您应该写
if (Integer.parseInt(gs2) == numToGuess)
or equivalently: 或等效地:
if (gs2.equals(String.valueOf(numToGuess))
Since you want to compare gs2
with numToGuess
they need to be of the same type. 由于要进行比较
gs2
与numToGuess
他们需要的是同一类型的。 So either you change gs2
to an Integer
using Integer.parseInt()
or you change numToGuess
to a String
using String.valueOf()
因此,无论你改变
gs2
一个Integer
使用Integer.parseInt()
或更改numToGuess
一个String
使用String.valueOf()
You have to move the declarations of the labels and text-fields from the constructor to GuessTheNumber. 您必须将标签和文本字段的声明从构造函数移动到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);
//...
Otherwise you only have access to them in the constructor. 否则,您只能在构造函数中访问它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.