我正在为 Java 作业做猜谜游戏,我遇到了嵌套 if else 的逻辑问题。我试图确定两件事:数字是高/低,以及用户是否变得更热/更冷。我有一个变量 intGuess 和 intOldGuess。我试图比较两者的热/冷,但我要么最终得到 intOldGuess 为 0,要么在比较时与 intGuess 相同。当然,两者都不正确。这是我到目前为止所拥有的:
这是我认为给我带来麻烦的部分:
public void actionPerformed(ActionEvent e)
{
int intGuess = 0, intOldGuess;
intOldGuess = intGuess;
try
{
intGuess = Integer.parseInt(txaUserGuess.getText().trim());
if (intGuess < intRandomNum)
{
lblHighLow.setText("Too Low");
if (intGuess > intOldGuess)
{
lblHotCold.setText("Getting Colder");
System.out.println(intOldGuess);
System.out.println(intGuess);
}
如果您需要,这是完整的代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class GuessNumber extends JFrame
{
private JTextField txaUserGuess;
private JLabel lblStart, lblPrompt, lblHighLow, lblHotCold;
private JButton btnGuess, btnQuit, btnAgain;
private int intRandomNum;
Container c = getContentPane();
public static void main(String args[])
{
GuessNumber app = new GuessNumber();
}
public GuessNumber()
{
super("Guessing Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Creates components
btnGuess = new JButton("Guess");
btnQuit = new JButton("Quit");
btnAgain = new JButton("Play Again?");
lblStart = new JLabel("I have a number between 1 and 1000.");
lblPrompt = new JLabel(
"Can you guess my number? Please enter your guess:");
lblHighLow = new JLabel("");
lblHotCold = new JLabel("");
txaUserGuess = new JTextField(5);
setLayout(new FlowLayout());
c.add(lblStart);
c.add(lblPrompt);
c.add(txaUserGuess);
c.add(btnGuess);
c.add(btnQuit);
c.add(btnAgain);
c.add(lblHighLow);
c.add(lblHotCold);
setSize(350, 200);
setVisible(true);
btnAgain.setVisible(false);
RandomNumber();
FirstGuessButtonHandler ghandler = new FirstGuessButtonHandler();
btnGuess.addActionListener(ghandler);
QuitButtonHandler qhandler = new QuitButtonHandler();
btnQuit.addActionListener(qhandler);
AgainButtonHandler ahandler = new AgainButtonHandler();
btnAgain.addActionListener(ahandler);
}
private void RandomNumber()
{
intRandomNum = new Random().nextInt(1000) + 1;
System.out.println(intRandomNum);
}
class QuitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
class AgainButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
class FirstGuessButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int intGuess = 0, intOldGuess;
intOldGuess = intGuess;
try
{
intGuess = Integer.parseInt(txaUserGuess.getText().trim());
if (intGuess < intRandomNum)
{
lblHighLow.setText("Too Low");
if (intGuess > intOldGuess)
{
lblHotCold.setText("Getting Colder");
System.out.println(intOldGuess);
System.out.println(intGuess);
} else if (intGuess < intOldGuess)
{
lblHotCold.setText("Getting Hotter");
}
} else if (intGuess > intRandomNum)
{
lblHighLow.setText("Too High");
} else
{
c.setBackground(Color.GREEN);
lblHighLow.setText("\n\tCorrect!");
txaUserGuess.setEditable(false);
btnAgain.setVisible(true);
btnGuess.setEnabled(false);
}
} catch (NumberFormatException e1)
{
lblHighLow.setText("\n\tEnter a VALID number!");
}
}
}
}