好的,這是猜謎遊戲完整代碼的一部分。找不到猜錯遊戲類錯誤
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
}
}
}
}
}
}
輸出爲:
.........................
玩? Y/N
ý
猜測目標整數足夠接近1和100(含)
猜之間
再次44 6
,你想繼續猜測? Y/N猜測目標整數是1和100(含)
之間 ..........................
問題是,當用戶猜測一個數字,條件是如果猜測和生成的數字之間的差異是5或更小,告訴用戶它足夠接近,並詢問用戶是否想繼續猜測,但條件wasn沒有實現,但仍然運行,有人可以幫忙嗎?
非常感謝!即時看着它現在。 – Liquified 2012-04-23 17:32:24
還有一個問題,while條件之前,有一條線路沒有工作,String play = scan.nextLine();命令根本不起作用,因爲我還沒有發現的原因,並且讓我瘋狂...... – Liquified 2012-04-23 17:42:36