我试图完成这个游戏。以下代码就是我现在所处的位置。你需要猜测一个介于1和100之间的数字。假设数字为50,你首先猜测是40.如果你的下一个猜测是39,它应该告诉你你变得越来越冷。我在底部附近有一些if语句没有按照他们需要的方式执行。它告诉你你变得越来越冷的唯一方法是你猜数字是什么,而不是数字。我不知道什么是错的。
如果您运行代码我已修改它以便更容易进行故障排除。在底部,您将得到正确的答案。右边是currentDistance
import java.awt.*;
import java.awt.event.*;
import java.awt.BorderLayout.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.util.Random;
import java.io.*;
import helpers.*;
public class GuessGame extends JPanel{
private static int GUESS_MAX = 100;
private static int GUESS_MIN = 0;
private static int WINDOW_HEIGHT = 300;
private static int WINDOW_WIDTH = 325;
private int lastDistance;
private int currentDistance;
private int guessCount = 0;
private Random random = new Random();
private int numberToGuess = 0;
private JFrame mainFrame;
private JButton tryButton;
private JButton newGameButton;
private ExitButton exitButton;
private JLabel guessLabel;
private JTextField guessField;
private JLabel headerLabel;
private JLabel countLabel;
private JLabel alertLabel;
private JPanel panel;
public GuessGame(){
numberToGuess = 1 + random.nextInt(100);
mainFrame = new JFrame("Number Guessing Game");
mainFrame.setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel(new GridBagLayout());
mainFrame.getContentPane().add(panel, BorderLayout.NORTH);
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(10, 10, 10,10);
tryButton = new JButton("Try the number");
newGameButton = new JButton("New Game");
exitButton = new ExitButton();
headerLabel = new JLabel("<html>I have a whole number between 1 and 100,<br>can you guess the number?</html>", SwingConstants.CENTER);
guessLabel = new JLabel("Enter a guess:");
guessField = new JTextField(5);
countLabel = new JLabel("# of guesses: 0");
tryButton = new JButton("Try the number");
alertLabel = new JLabel("Awaiting Input");
newGameButton = new JButton("New Game");
exitButton = new ExitButton();
c.gridx = 0;
c.gridy= 0;
c.gridwidth = 2;
panel.add(headerLabel, c);
c.gridwidth = 1;
c.gridx = 0;
c.gridy= 1;
panel.add(guessLabel, c);
c.gridx = 1;
c.gridy= 1;
panel.add(guessField, c);
c.gridx = 0;
c.gridy= 2;
c.gridwidth = 2;
panel.add(countLabel, c);
c.gridx = 0;
c.gridy= 3;
panel.add(tryButton, c);
c.gridx = 0;
c.gridy= 4;
panel.add(alertLabel, c);
c.gridwidth = 1;
c.gridx = 0;
c.gridy= 5;
panel.add(newGameButton, c);
c.gridx = 1;
c.gridy= 5;
panel.add(exitButton, c);
newGameButton.setMnemonic('N');
NewGameButtonHandler nghandler = new NewGameButtonHandler();
newGameButton.addActionListener(nghandler);
tryButton.setMnemonic('T');
TryButtonHandler tryhandler = new TryButtonHandler();
tryButton.addActionListener(tryhandler);
mainFrame.setVisible(true);
}
class NewGameButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
numberToGuess = 1 + random.nextInt(100);
countLabel.setText("# of guesses: 0");
alertLabel.setForeground(Color.BLACK);
alertLabel.setText("Awaiting Input");
guessField.setText("");
guessCount = 0;
tryButton.setVisible(true);
}
}
class TryButtonHandler implements ActionListener
{
int lastDist = lastDistance;
public void actionPerformed(ActionEvent e)
{
int guess;
boolean correct = false;
String instring = guessField.getText();
if (guessCount == 0)
{
lastDistance = GUESS_MAX;
}
guess = Integer.parseInt(instring);
if(guess >= GUESS_MIN && guess <= GUESS_MAX)
{
{
guessCount= guessCount + 1;
countLabel.setText("# of guesses: " + guessCount);
}
if (guess > numberToGuess)
{
alertLabel.setText("Too high. ");
}
else if (guess < numberToGuess)
{
alertLabel.setText("Too low. ");
}
else
{
alertLabel.setText("Correct!");
correct = true;
alertLabel.setForeground(Color.GREEN);
tryButton.setVisible(false);
}
}
if (correct == false)
{
currentDistance = Math.abs((guess - numberToGuess));
if (currentDistance <= lastDist && guessCount !=1)
{
alertLabel.setForeground(Color.RED);
alertLabel.setText(alertLabel.getText() + " Getting warmer");
}
else if(currentDistance >= lastDist && guessCount !=1)
{
alertLabel.setForeground(Color.BLUE);
alertLabel.setText(alertLabel.getText() + " Getting colder");
}
}
lastDist = currentDistance;
}
}
public static void main(String[] args) {
new GuessGame();
}
}
答案 0 :(得分:2)
我看到的一个问题是,if (correct == false)
可能后跟括号,因为您只想这样做
如果当前猜测错误,则显示Getting Warmer / Colder消息。
if (correct == false) {
currentDistance = Math.abs((guess - numberToGuess));
if (currentDistance <= lastDistance)
{
alertLabel.setForeground(Color.RED);
alertLabel.setText(" Getting warmer");
cheat2.setText(String.valueOf(currentDistance));
}
else
{
alertLabel.setForeground(Color.BLUE);
alertLabel.setText("Getting colder");
cheat2.setText(String.valueOf(currentDistance));
}
lastDistance = currentDistance;
}
此外,看起来每次猜测时都会初始化一些变量:
int guessCount = 0;
if (guessCount == 0)
{
lastDistance = GUESS_MAX;
}
只有在名称游戏开始时才应该进行初始化。
答案 1 :(得分:1)
我想我解决了你的问题,至少它现在有效,但总会有某个地方出现错误。 所以我基本上重写了你的try按钮处理程序,所以它现在可以工作我希望你能找到变化并理解它们,如果不能,你总能问。
我所做的更改包含在//Me start
和//Me end
之间。
我还必须编辑一些内容以使错误消失,但这些更改都标记为TODO,因为您可能在项目中有相应的类。
package Week5;
import java.awt.*;
import java.awt.event.*;
import java.awt.BorderLayout.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.util.Random;
import java.io.*;
//import helpers.*;TODO this import was no found but it is probably in your project
public class GuessGame extends JPanel{
private static int GUESS_MAX = 100;
private static int GUESS_MIN = 0;
private static int WINDOW_HEIGHT = 400;
private static int WINDOW_WIDTH = 325;
private Random random = new Random();
private int numberToGuess = 0;
private JFrame mainFrame;
private JButton tryButton;
private JButton newGameButton;
private ExitButton exitButton;
private JLabel guessLabel;
private JTextField guessField;
private JLabel headerLabel;
private JLabel countLabel;
private JLabel alertLabel;
private JPanel panel;
private JLabel cheat;
private JLabel cheat2;
public GuessGame(){
numberToGuess = 1 + random.nextInt(100);
mainFrame = new JFrame("Number Guessing Game");
mainFrame.setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel(new GridBagLayout());
mainFrame.getContentPane().add(panel, BorderLayout.NORTH);
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(10, 10, 10,10);
tryButton = new JButton("Try the number");
newGameButton = new JButton("New Game");
exitButton = new ExitButton();
headerLabel = new JLabel("<html>I have a whole number between 1 and 100,<br>can you guess the number?</html>", SwingConstants.CENTER);
guessLabel = new JLabel("Enter a guess:");
guessField = new JTextField(5);
countLabel = new JLabel("# of guesses: 0");
tryButton = new JButton("Try the number");
alertLabel = new JLabel("Awaiting Input");
newGameButton = new JButton("New Game");
exitButton = new ExitButton();
cheat = new JLabel(String.valueOf(numberToGuess));
cheat2 = new JLabel("no value");
c.gridx = 0;
c.gridy= 0;
c.gridwidth = 2;
panel.add(headerLabel, c);
c.gridwidth = 1;
c.gridx = 0;
c.gridy= 1;
panel.add(guessLabel, c);
c.gridx = 1;
c.gridy= 1;
panel.add(guessField, c);
c.gridx = 0;
c.gridy= 2;
c.gridwidth = 2;
panel.add(countLabel, c);
c.gridx = 0;
c.gridy= 3;
panel.add(tryButton, c);
c.gridx = 0;
c.gridy= 4;
panel.add(alertLabel, c);
c.gridwidth = 1;
c.gridx = 0;
c.gridy= 5;
panel.add(newGameButton, c);
c.gridx = 1;
c.gridy= 5;
panel.add(exitButton, c);
c.gridx = 0;
c.gridy = 6;
panel.add(cheat, c);
c.gridx = 1;
c.gridy = 6;
panel.add(cheat2, c);
newGameButton.setMnemonic('N');
NewGameButtonHandler nghandler = new NewGameButtonHandler();
newGameButton.addActionListener(nghandler);
tryButton.setMnemonic('T');
TryButtonHandler tryhandler = new TryButtonHandler();
tryButton.addActionListener(tryhandler);
mainFrame.setVisible(true);
}
//Me start - TODO this class was missing but you probably have it so just remove this.
class ExitButton extends JButton{
}
//Me end
class NewGameButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
numberToGuess = 1 + random.nextInt(100);
countLabel.setText("# of guesses: 0");
alertLabel.setForeground(Color.BLACK);
alertLabel.setText("Awaiting Input");
guessField.setText("");
cheat.setText(String.valueOf(numberToGuess));
//Me start
guessCount = 0;
lastDist = 0;
//Me end
}
}
//Me start
static int guessCount = 0;
static int lastDist = 0;
class TryButtonHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
int guess;
try{
guess = Integer.parseInt(guessField.getText());
}catch(NumberFormatException e){
alertLabel.setText("That is not a number!");
return;
}
if(guess < GUESS_MIN || guess > GUESS_MAX){
alertLabel.setText("That number is out of range!");
lastDist = distance(guess);
guessCount++;
return;
}
if(guessCount == 0){
if(guess > numberToGuess){
alertLabel.setText("Too high. ");
}else if(guess < numberToGuess){
alertLabel.setText("Too low. ");
}
lastDist = distance(guess);
guessCount++;
return;
}
if(guess == numberToGuess){
alertLabel.setText("Correct!");
alertLabel.setForeground(Color.GREEN);
return;
}
int dist = distance(guess);
if(dist < lastDist){
alertLabel.setForeground(Color.RED);
alertLabel.setText(" Getting warmer");
}else if(dist > lastDist){
alertLabel.setForeground(Color.BLUE);
alertLabel.setText("Getting colder");
}else{
alertLabel.setForeground(Color.BLACK);
alertLabel.setText("Why did you enter the same number?");
}
lastDist = dist;
cheat2.setText(String.valueOf(guess));
guessCount++;
}
private int distance(int guess){
return numberToGuess - guess < 0 ? guess - numberToGuess : numberToGuess - guess;
}
}
//Me end
public static void main(String[] args) {
new GuessGame();
}
}
我希望这有效:)。
P.S。顺便说一下,distance是我在按钮处理程序中添加的一种方法。