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
}
}
//当我运行我的程序时,当我输入我的猜测号码时会弹出消息框,但是它似乎停止了,没有其他任何东西弹出来让我知道如果我猜到高或低。
答案 0 :(得分:0)
采取这些行,例如:
JOptionPane.showInputDialog("Guess a number between 1 and 32: ");
guess = input.nextInt();
会出现一个提示,要求您输入1到32之间的数字&#34;。因此,您输入数字,然后执行停止,因为您的Scanner
正在等待您的输入。您需要完全删除扫描仪才能避免这些频繁的中断。
showInputDialog
的{{1}}方法会返回JOptionPane
,如Javadocs中的示例所示:
显示一个对话框,要求用户输入字符串:
String
按原样,您应该丢弃从方法返回的值,而应该将其分配给变量。要将返回的值分配给String inputValue = JOptionPane.showInputDialog("Please input a value");
变量int
,请使用guess
:
Integer.parseInt
答案 1 :(得分:0)
JOptionPane.showInputDialog返回String,而不是每次收到输入时都要使用Parse方法将String转换为int。
String input = JOptionPane.showInputDialog();
Int number = Integer.parseInt(input);