public boolean ChecktheNum()
{
if (number == (int)number && number <= max && number >= 1)
{
return true;
}
return false;
}
do
{
String num2=JOptionPane.showInputDialog(" Guess a integer between 1 and "+max);
int max2 = Integer.parseInt(num2);
GuesstheNum game= new GuesstheNum(max2);
game.ChecktheNum();
} while (game.ChecktheNum == false)
我的测试人员文件和do while循环都出现问题。
出现错误.game
是无法找到的符号
为什么是这样?
即使我尝试了break方法,错误仍然会出现
另外我需要测试输入的数字是否是整数,这个函数也没有工作,因为它不是要求另一个输入
答案 0 :(得分:1)
do{
...
GuesstheNum game= new GuesstheNum(max2);
game.ChecktheNum();
}while(game.ChecktheNum == false)
game
不在while
的范围内。你可以这样做
GuesstheNum game;
do{
...
game= new GuesstheNum(max2);
game.ChecktheNum();
}while(game.ChecktheNum == false)
编辑:尝试这样的事情
GuesstheNum game;
boolean guess;
do {
String num2 = JOptionPane.showInputDialog(" Guess a integer between 1 and " + max);
int max2 = Integer.parseInt(num2);
game = new GuesstheNum(max2);
guess = game.ChecktheNum();
} while (guess);
更简单地说
GuesstheNum game;
do {
String num2 = JOptionPane.showInputDialog(" Guess a integer between 1 and " + max);
int max2 = Integer.parseInt(num2);
game = new GuesstheNum(max2);
} while (game.ChecktheNum());
编辑:使用整数检查
GuesstheNum game;
do {
String num2 = JOptionPane.showInputDialog(" Guess a integer between 1 and " + max);
int max2;
try{
int max2 = Integer.parseInt(num2);
} catch (NumberFormatException ex){
continue;
}
game = new GuesstheNum(max2);
} while (!game.ChecktheNum());