好的,这是猜谜游戏完整代码的一部分。
public static void Game(){ //Second page of game with option of continue playing when guessed close enough to answer.
Scanner scan = new Scanner(System.in);
GuessingGame testgame=new GuessingGame();
testgame.Generator();
int num = testgame.GetGenNum();
//Produces a random number between 1 and 100 inclusive of 1 and 100
System.out.println("Guess the target integer which is between 1 and 100 inclusive");
int guess=scan.nextInt();
int difference=(guess-num);
while(guess!=num){
int yesCounter=0;
System.out.println("Guess again " +num+" "+difference);
guess=scan.nextInt();
difference=(guess-num);
///something wrong here, shouldnt repeat if difference too big
if(difference<=5||difference>=-5){ //something wrong with the condition, it says its close enough even tho it isnt.
while(yesCounter<1){
System.out.println("Close enough, do you want to keep Guessing? Y/N ");
yesCounter++;
String play = scan.nextLine();
//play=play1;
while(!(play.equalsIgnoreCase("y")))
{
if(play.equalsIgnoreCase("n")){
System.exit(1);
}
else{
if((play.equalsIgnoreCase("y"))){
invalid();
guess=scan.nextInt();
}
else{
Game(); ///TAKE note as it might restart the game with new random integer
}
}
}
}
}
}
输出是:
…………………………………………………………………………………………………………
玩?是/否
是的
猜测 1 到 100 之间的目标整数
50
再猜一次 44 6
44
够近了,你要继续猜吗?是/否
猜测 1 到 100 之间的目标整数
…………………………………………………………………………………………
问题是,当用户猜测一个数字时,条件是如果猜到的数字与生成的数字之间的差异小于或等于5,告诉用户它足够接近,并询问用户是否要继续猜测,但条件不是完成但仍在运行,有人可以帮忙吗?