GUI猜测游戏 - 如何正确编程猜测

时间:2013-08-15 15:59:11

标签: java

我是这个网站的新手。 下面我已经把我的代码。我的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 "));
            }


            }
        }
    }
}   

3 个答案:

答案 0 :(得分:4)

int getguessInput = 0;

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

您应该从actionlistener外部访问这些int,并确保实际设置了getGuessInput。

这种实施的血腥细节留给读者练习。

答案 1 :(得分:2)

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

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

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

答案 2 :(得分:0)

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

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

我的java不是很尖锐但它应该是这样的。

int getguessinput = Integer.parseInt(guessInput.getText());
相关问题