重新提示对话框输入,直到符合猜测数-java
我一直在尝试提示相同的输入对话框,直到用户猜出正确的数字,但我不知道如何,任何帮助都将不胜感激!我很确定如何实现while循环,但我不知道具体如何实现。用户输入是解析为int的字符串,用于计算重新提示对话框输入,直到符合猜测数-java,java,input,while-loop,dialog,prompt,Java,Input,While Loop,Dialog,Prompt,我一直在尝试提示相同的输入对话框,直到用户猜出正确的数字,但我不知道如何,任何帮助都将不胜感激!我很确定如何实现while循环,但我不知道具体如何实现。用户输入是解析为int的字符串,用于计算 JOptionPane.showMessageDialog(null, "\nWelcome to Guess- My-Number\nA GAME of CHANCE and SKILL\nLet's Play!\n\n"); // input number
JOptionPane.showMessageDialog(null, "\nWelcome to Guess- My-Number\nA GAME of CHANCE and SKILL\nLet's Play!\n\n");
// input number
String myGuess = JOptionPane.showInputDialog("Enter a number between 1 and 25:");
int myGuess_int = Integer.parseInt(myGuess);
final int NUMBER_TO_GUESS = 13;
//good or bad guess??
while(myGuess_int != NUMBER_TO_GUESS)
{
if(myGuess_int < NUMBER_TO_GUESS) //too low
{
String message = String.format("Your guess [ %s ] is too low...\nTry Again!\n", myGuess);
JOptionPane.showMessageDialog(null, message);
}
else //too high
{
String message = String.format("Your guess [ %s ] is too high...\nTry Again!\n", myGuess);
JOptionPane.showMessageDialog(null, message);
}
JOptionPane.showInputDialog("Enter a number between 1 and 25:");
myGuess_int = Integer.parseInt(myGuess);
}//end while
String message = String.format("Your guess [ %s ] is the number...\nCongratulations!\n", myGuess);
JOptionPane.showMessageDialog(null, message);
} //end main
} //end class
JOptionPane.showMessageDialog(空,“\n大家来猜一猜-我的号码\n一个机会和技能的游戏\n网的游戏!\n\n”);
//输入号码
字符串myGuess=JOptionPane.showInputDialog(“输入一个介于1和25之间的数字:”);
int myGuess_int=Integer.parseInt(myGuess);
最终整数_至_猜测=13;
//猜对还是猜错??
while(myGuess_int!=数字到猜测)
{
if(myGuess\u int
您可以在条件中使用do while循环
场景如下:
do {
// accept prompt from user
// display error message using if else statement
} while (myGuess_int != NUMBER_TO_GUESS); // loop again if input not match
您可以在条件中使用do while循环 场景如下:
do {
// accept prompt from user
// display error message using if else statement
} while (myGuess_int != NUMBER_TO_GUESS); // loop again if input not match
试着使用一个新的工具
JOptionPane.showMessageDialog(空,“\n大家来猜一猜-我的号码\n一个机会和技能的游戏\n网的游戏!\n\n”);
最终整数_至_猜测=13;
//猜对还是猜错??
字符串myGuess;
int myGuess_int;
做{
//输入号码
myGuess=JOptionPane.showInputDialog(“输入一个介于1和25之间的数字:”);
myGuess_int=Integer.parseInt(myGuess);
if(myGuess\u int
尝试使用
JOptionPane.showMessageDialog(空,“\n大家来猜一猜-我的号码\n一个机会和技能的游戏\n网的游戏!\n\n”);
最终整数_至_猜测=13;
//猜对还是猜错??
字符串myGuess;
int myGuess_int;
做{
//输入号码
myGuess=JOptionPane.showInputDialog(“输入一个介于1和25之间的数字:”);
myGuess_int=Integer.parseInt(myGuess);
if(myGuess\u int
是的,我尝试了一个do while循环,但是我得到了这个错误:错误:找不到符号while(myGuess\u int!=NUMBER\u TO\u GUESS);^symbol:variable myGuess_int location:class Guess
现在编辑,应该能够访问它,因为它是在外部作用域中声明的。我运行时运气不好,当我在do while循环外部声明变量时,编译器显示错误:错误:找不到symbol myGuess_int=Integer.parseInt(myGuess)代码>所以我认为它不会让我在do while循环之外声明…谢谢你的时间!是的,我尝试了一个do while循环,但是我得到了这个错误:error:找不到符号while(myGuess\u int!=NUMBER\u TO\u GUESS);^symbol:variable myGuess_int location:class Guess
现在编辑,应该能够访问它,因为它是在外部作用域中声明的。我运行时运气不好,当我在do while循环外部声明变量时,编译器显示错误:错误:找不到symbol myGuess_int=Integer.parseInt(myGuess)代码>所以我认为它不会让我在do while循环之外声明…谢谢你的时间!