使用自定义按钮制作数字猜谜游戏

时间:2016-05-04 05:31:59

标签: java

我正在尝试制作一个不断猜测数字的程序,询问用户数字是太高还是太低,并调整猜测直到猜到正确的数字并且用户说程序找到了正确的数字。然后它返回猜测数字所花费的次数。

我的问题是,我不确定如何将不同的自定义按钮连接到不同的方法来调整猜测

public static void main(String[] args) {

    int result; 
    String setUpper = JOptionPane.showInputDialog("Please set the upper bound");
    Scanner input;
    input = new Scanner(setUpper); 
    int upperBound;
    upperBound = input.nextInt();
    int [] guessArray = new int[upperBound]; 

    int left;   // Left edge of the unsearched portion
    int right;  // just past right edge of unsearched portion
    left = 0;
    right = guessArray.length;
    counter = 0; 


    JOptionPane.showMessageDialog(null, "Alright, I'll find the number you picked, from 0 to " + guessArray.length, "Output", JOptionPane.INFORMATION_MESSAGE);

    Object[] options = {"Too low", "You got it!", "Too high"};

    result = JOptionPane.showOptionDialog(null, "Is your number " + (guessArray.length/2), "Guess",
        JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
        null, options, options[0]); 

    while (left != right-1) { // while left, right not adjacent
        int mid = (right+left)/2; 
        counter++;
        //guessArray[mid]; 

        if (result == JOptionPane.YES_OPTION) {  // it's to the left
            right = mid;

        }
        else { 
            if (result == JOptionPane.CANCEL_OPTION) {
                left = mid;
            }
            else {
                if (result == JOptionPane.NO_OPTION) {
                    JOptionPane.showMessageDialog(null, "It took " + counter + " comparisons to find your number",  
                        "Output", JOptionPane.INFORMATION_MESSAGE);
                }

            }

        }
    }

} 

2 个答案:

答案 0 :(得分:0)

使用JOptionPane,无法将方法绑定到按钮。它会为您提供返回值并阻止调用者,直到出现用户交互。

有关详细信息,请参阅文档:Java-Doc

要将方法绑定到按钮,可以使用普通的JButtons,或者如果您想查看JavaFX-Button类JavaFX。这是通过侦听组件上的事件来实现的。一个好的解决方案可能是在标签或文本字段中显示程序问题和答案消息,并在其下面放置“是”和“否”按钮 - 通过单击此按钮,您的操作将被执行。

答案 1 :(得分:0)

如果您定义了3个按钮,则JOptionPane返回零,一或两个。

您的代码甚至无法编译。修复编译错误后,用于计算下一个数字的按钮代码是向后的。最后,我在do while循环中提取了JOptionPane代码,因此该人多次看到JOptionPane。

这是更正后的代码。花点时间尝试了解我对您的代码所做的工作。

package com.ggl.testing;

import java.util.Scanner;

import javax.swing.JOptionPane;

public class NumberGuessing {

    public static void main(String[] args) {

        int result;
        String setUpper = JOptionPane
                .showInputDialog("Please set the upper bound");
        Scanner input;
        input = new Scanner(setUpper);
        int upperBound;
        upperBound = input.nextInt();
        int[] guessArray = new int[upperBound];

        int left; // Left edge of the unsearched portion
        int right; // just past right edge of unsearched portion
        left = 0;
        right = guessArray.length;
        int counter = 0;
        int mid = guessArray.length / 2;

        input.close();

        JOptionPane.showMessageDialog(null,
                "Alright, I'll find the number you picked, from 0 to "
                        + (guessArray.length - 1), "Output",
                JOptionPane.INFORMATION_MESSAGE);

        Object[] options = { "Too low", "You got it!", "Too high" };

        do { // while not correct number

            result = JOptionPane.showOptionDialog(null,
                    "Is your number " + mid, "Guess",
                    JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
                    null, options, options[0]);

            // result is zero, number is larger
            if (result == JOptionPane.YES_OPTION) {
                left = mid;
                mid = (right + left) / 2;
                counter++;
                // result is 2, number is smaller
            } else if (result == JOptionPane.CANCEL_OPTION) {
                right = mid;
                mid = (right + left) / 2;
                counter++;
            }

        } while (result != 1);

        JOptionPane.showMessageDialog(null, "It took " + counter
                + " comparisons to find the number " + mid, "Output",
                JOptionPane.INFORMATION_MESSAGE);

    }

}