-1

我是这个网站的新手。下面我放了我的代码。我的 GUI 上的一个小背景——这是一个猜谜游戏。计算机已经想到了一个介于 1 到 100 之间的随机数,用户必须尝试猜测它。当用户输入一个数字时,程序必须告诉用户计算机正在考虑的数字是低于还是高于或等于用户猜测的数字。用户有 8 次尝试猜数字。如果用户在 8 次尝试中未能猜出数字,程序将向用户显示该数字并自动关闭。我的问题是 - 你能告诉我为什么游戏的猜测部分不起作用吗?即,当我输入 100 作为我想猜的数字时,程序会说“猜得更高”,而且,程序没有正确地告诉你更高、更低或相等。谢谢!

import java.awt.Dimension;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.awt.Font;
import java.awt.LayoutManager;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

    public class Board extends JPanel{
    private int guessCount;
    private JLabel JLabel1 = new JLabel("Guessing Game");
    private JLabel JLabel2 = new JLabel("The computer will think of a number between");
    private JLabel JLabel3 = new JLabel(" 1 and 100 and you will try to guess it.");
    private JLabel JLabel5 = new JLabel("If you do not get the number within 8 guesses,");
    private JLabel JLabel6 = new JLabel("the program will show you the number and will");
    private JLabel JLabel7 = new JLabel("automatically shut down.");
    private JLabel JLabel4 = new JLabel("Enter your guess: ");
    private JTextField guessInput = new JTextField();
    private JButton submit = new JButton();
     Board()
     {
        this.setLayout(new BorderLayout());
        JPanel subPanelTitle = new JPanel(); 

        JLabel1.setBackground(Color.WHITE); 
        JLabel1.setForeground(Color.GREEN); 
        Font font1 = new Font("Monospaced", Font.BOLD, 50);
        JLabel1.setFont(font1);
        JLabel2.setBackground(Color.WHITE); 
        JLabel2.setForeground(Color.MAGENTA); 
        Font font2 = new Font("Monospaced", Font.BOLD, 18);
        JLabel2.setFont(font2);
        JLabel3.setBackground(Color.WHITE); 
        JLabel3.setForeground(Color.MAGENTA); 
        Font font3 = new Font("Monospaced", Font.BOLD, 18);
        JLabel3.setFont(font3);
        JLabel4.setBackground(Color.WHITE); 
        JLabel4.setForeground(Color.BLUE); 
        Font font4 = new Font("Monospaced", Font.BOLD, 18);
        JLabel4.setFont(font4);
        JLabel5.setBackground(Color.WHITE);
        JLabel5.setForeground(Color.BLACK);
        Font font5 = new Font("Monospaced", Font.BOLD, 16);
        JLabel5.setFont(font5);
        JLabel6.setBackground(Color.WHITE);
        JLabel6.setForeground(Color.BLACK);
        JLabel6.setFont(font5);
        JLabel7.setBackground(Color.WHITE);
        JLabel7.setForeground(Color.BLACK);
        JLabel7.setFont(font5);
        guessInput.setPreferredSize(new Dimension(50,50));
        submit.setPreferredSize(new Dimension(100,20));
        submit.setForeground(Color.RED);
        submit.setText("Submit");
        subPanelTitle.add(JLabel1);
        subPanelTitle.add(JLabel2);
        subPanelTitle.add(JLabel3);
        subPanelTitle.add(JLabel5);
        subPanelTitle.add(JLabel6);
        subPanelTitle.add(JLabel7);
        subPanelTitle.add(JLabel4);
        subPanelTitle.add(guessInput);
        subPanelTitle.add(submit);
        subPanelTitle.setPreferredSize(new Dimension(600, 350));    
        guessCount = -1;
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(10, 10));

        this.add(subPanelTitle,BorderLayout.EAST);

        submit.addActionListener(new ButtonListener());
     }

        private class ButtonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
            String actionCommand = e.getActionCommand();
            Scanner sc=new Scanner(System.in);
            {

            Random generator = new Random();
            int max = 100;
            int min = 1;
            int random = generator.nextInt((max-min+1)-min);
            int getguessInput = 0;


            guessCount++;

            if(guessCount > 7){
                JOptionPane.showMessageDialog(null, "You are out of guesses. You lose. The number was " + random);
                System.exit(0);
            }
            if(getguessInput < random){
                JOptionPane.showMessageDialog(null, ("Guess higher"));
            }
            else if(getguessInput > random){
                JOptionPane.showMessageDialog(null, ("Guess lower"));
            }
            else{
                JOptionPane.showMessageDialog(null, ("Congratulations, YOU WIN! " + " It took you " + guessCount + " guesses "));
            }


            }
        }
    }
}   
4

3 回答 3

4
int getguessInput = 0;

永远不会改变。您总是在处理 0 的猜测,并且guessCount也会被重置。

您应该使这些整数可以从动作侦听器外部访问,并确保实际设置了 getGuessInput。

这个实现的血淋淋的细节留给读者作为练习。

于 2013-08-15T16:01:13.260 回答
2

在每次提交时,您都会一次又一次地执行 init 例程:

        Random generator = new Random();
        int max = 100;
        int min = 1;
        int random = generator.nextInt((max-min+1)-min);
        int getguessInput = 0;

这应该做一次,例如在类的构造函数中。

于 2013-08-15T16:04:50.037 回答
0

随机数应该生成一次,而不是每次用户点击按钮。生成的整个代码块需要从您的按钮单击事件中移出。

getguessInput应该从用户输入的每次尝试中检索该值,而不是在每次按钮单击时设置为 0。

我的java不是很锋利,但应该是这样的。

int getguessinput = Integer.parseInt(guessInput.getText());
于 2013-08-15T16:10:48.260 回答