import java.util.Random;
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.util.*;

public class GuessingGameGUI {

    public static void main(String[] args) {
        Random rand = new Random();
        int value = rand.nextInt(32) + 1;
        int guesses = 0, LowGuess = 0, HighGuess = 0; //ADDED LowGuess AND HighGuess
        Scanner input = new Scanner(System.in);
        int guess;
        boolean win = false;
        while (win == false) {
            JOptionPane.showInputDialog("Guess a number between 1 and 32: ");
            guess = input.nextInt();
            guesses++;
            if (guess == value) {
                win = true;
            } else if (guess < value) {
                JOptionPane.showInputDialog("Your guess is lower than random number");
                LowGuess++; //COUNTER TO KEEP TRACK OF LOW GUESSES
            } else if (guess > value) {
                JOptionPane.showInputDialog("Your guess is higher than random number");
                HighGuess++; //COUNTER TO KEEP TRACK OF HIGH GUESSES
            }
        }
        JOptionPane.showMessageDialog(null, "You win! You are the worst guesser in history!");
        JOptionPane.showMessageDialog(null, "The number was: " + value);

// ADDED FROM HERE (GUESSES)
        JOptionPane.showMessageDialog(null, "Number of Guesses:" + guesses);
        for (int x = 1; x <= guesses; x++) {
            for (int a = 1; a <= guesses; a++) {
                if (a == guesses) {
                    JOptionPane.showMessageDialog(null, "*");
                }
            }
        } // TO HERE FOR AMOUNT OF GUESSES

// I ADDED FROM HERE (LOW)
        JOptionPane.showMessageDialog(null, "Smaller Guesses:" + LowGuess);
        for (int Low_i = 1; Low_i <= LowGuess; Low_i++) {
            for (int Low_e = 1; Low_e <= LowGuess; Low_e++) {
                if (Low_e == LowGuess) {
                    JOptionPane.showMessageDialog(null, "*");
                }
            }
        } // Then TO HERE FOR LOW

// I ADDED FROM HERE (HIGH)
        JOptionPane.showMessageDialog(null, "Largest Guesses:" + HighGuess);
        for (int High_i = 1; High_i <= HighGuess; High_i++) {
            for (int High_e = 1; High_e <= HighGuess; High_e++) {
                if (High_e == HighGuess) {
                    JOptionPane.showMessageDialog(null, "*");
                }
            }
        } //FINALLY TO HERE FOR HIGH
    }
}


//当我运行程序时,输入我的猜测数字时会弹出消息框,但是它似乎停止了,并且没有其他弹出窗口让我知道我猜测的是高还是低。

最佳答案

以这些行为例:

JOptionPane.showInputDialog("Guess a number between 1 and 32: ");
guess = input.nextInt();


出现提示,要求您“猜测1到32之间的数字”。因此,您输入数字,然后执行就会停止,因为Scanner正在等待输入。您需要完全删除扫描仪,以免出现频繁的打扰。

showInputDialogJOptionPane方法返回一个String,如Javadocs中的示例所示:


  显示一个对话框,要求用户输入字符串:

String inputValue = JOptionPane.showInputDialog("Please input a value");



照原样,当您应该将方法返回的值分配给变量时,您将丢弃该方法。为了将返回值分配给您的int变量guess,请使用Integer.parseInt

guess = Integer.parseInt(JOptionPane.showInputDialog("Guess a number between 1 and 32: "));

09-28 05:06