【问题标题】:How to use the JOption on my guessing game?如何在我的猜谜游戏中使用 JOption?
【发布时间】:2015-04-18 13:03:58
【问题描述】:
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
    }
}

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

【问题讨论】:

    标签: java swing joptionpane


    【解决方案1】:

    以这些行为例:

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

    会显示一个提示,要求您“猜一个 1 到 32 之间的数字”。因此,您输入数字,然后执行停止,因为您的 Scanner 正在等待您的输入。您需要完全移除您的扫描仪,这样您就不会经常出现这些中断。

    JOptionPaneshowInputDialog 方法返回一个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: "));
    

    【讨论】:

      【解决方案2】:

      JOptionPane.showInputDialog 返回 String,而不是每次收到输入时都必须使用 Parse 方法将该 String 转换为 int。

      字符串输入 = JOptionPane.showInputDialog();

      整数 = Integer.parseInt(input);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-04-15
        • 2020-11-22
        • 2014-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多