2015-10-09 134 views
0

新到Java並想提出一個猜謎遊戲在用戶有猜測0和50當計數器不能正常工作

之間的隨機整數我試圖猜測添加計數器在「遊戲結束」之前給用戶高達7的遊戲猜測然而,即使我的while循環,櫃檯仍然保持在7以上。

我提前道歉,如果這是一個重複的問題,但是我也有一個簡單的介紹一下,但沒有找到一個很好的答案對我的代碼

繼承人的代碼:

package guessinggame; 
import java.util.Random; 
import java.util.Scanner; 
public class GuessingGame { 
//Author TC13551 


public static void main(String[] args) { 
    int NumberGuess = 0; 
    Random randomNumber = new Random(); 
    int randomInt = randomNumber.nextInt(51); 
    System.out.println("Guess the number between 0 -50:"); 
    int i = 0; 
    while(i < 8){ 
    do{ 

     Scanner UserGuess = new Scanner(System.in); 
     NumberGuess = UserGuess.nextInt(); 
     if(NumberGuess < 0 || NumberGuess > 50){ 
      System.out.println("Invalid Input, please enter numbers between 0 and 50"); 
     } 
     else if(NumberGuess < randomInt){ 
      System.out.println("Guess is too small."); 
      i ++; 
      System.out. println("You have made "+i +" guesses out of 7"); 
     } 

     else if(NumberGuess > randomInt) { 
      System.out.println("Guess is too big."); 
      i ++; 
      System.out. println("You have made "+i +" guesses out of 7"); 

     } 
      else if(NumberGuess == randomInt){ 
      System.out.println("Correct! You win!"); 
      System.out.println("It took you "+ i + " guesses."); 
      System.exit(0); 
     } 


    }while(randomInt != NumberGuess); 


}System.out.println("Game over!"); 
System.out.println("All 7 Guesses used!"); 
System.exit(0); 

    }} 
+1

ATLEAST提供你所面對的問題...... – afzalex

+0

此外,如果你得到的隨機數'0',你將永遠無法創建,因爲'randomInt輸入== 0'和'猜數字== 0' – SomeJavaGuy

回答

1

你應該有一個循環,如果你想在7個猜測終止:

do { 
    .... 
} while(randomInt != NumberGuess) && (i < 8); 

當你只能在外環測試i,你給用戶無限numbe猜測,因爲一旦用戶猜到了正確的數字,你就只剩下內部循環了。只有在你離開內部循環之後,你才能測試猜測的次數。

+0

三江源這解決了這個問題。 – Tom

+0

不要感謝,接受答案,並給他upvote。 – LoganMzz

0

您還可以檢查存在的條件下,如果不能是不是猜測和i值由下式給出機會超過(例如:7在你的情況下):

}while(i < 8); 

注:去除不必要的情況看@Rehman評論。
如果用戶猜到正確的號碼,您可以刪除randomInt != NumberGuess,因爲您有System.exit(0)

+0

爲什麼我們需要'randomInt!= NumberGuess'?在System.exit(0);'NumberGuess == randomInt'條件之前有一個檢查。 – Rehman

+0

感謝您指出,更新了答案 –

0

我認爲它可能解決您的查詢,

import java.util.Random; 
import java.util.Scanner; 
public class GuessingGame { 
    //Author TC13551 


    public static void main(String[] args) { 
     int NumberGuess = 0; 
     Random randomNumber = new Random(); 
     int randomInt = randomNumber.nextInt(51); 
     System.out.println("Guess the number between 0 -50:"); 
     int i = 0; 
      do{ 

       Scanner UserGuess = new Scanner(System.in); 
       NumberGuess = UserGuess.nextInt(); 
       if(NumberGuess < 0 || NumberGuess > 50){ 
        System.out.println("Invalid Input, please enter numbers between 0 and 50"); 
       } 
       else if(NumberGuess < randomInt){ 
        System.out.println("Guess is too small."); 
        i ++; 
        System.out. println("You have made "+i +" guesses out of 7"); 
       } 

       else if(NumberGuess > randomInt) { 
        System.out.println("Guess is too big."); 
        i ++; 
        System.out. println("You have made "+i +" guesses out of 7"); 

       } 
       else if(NumberGuess == randomInt){ 
        System.out.println("Correct! You win!"); 
        System.out.println("It took you "+ i + " guesses."); 
        System.exit(0); 
       } 


      }while(i < 8); 

      System.out.println("Game over!"); 
     System.out.println("All 7 Guesses used!"); 
     System.exit(0); 

    }} 
+0

我認爲您應該對您的修改添加一些解釋;) – LoganMzz

0
public class GuessingGame { 
    public static void main(String[] args) { 
     int NumberGuess = 0; 
     Random randomNumber = new Random(); 
     int randomInt = randomNumber.nextInt(51); 
     System.out.println("Guess the number between 0 -50:"); 
     int i = 1; 
     Scanner UserGuess = new Scanner(System.in); 

     while (i <= 7) { 

      NumberGuess = UserGuess.nextInt(); 
      if (NumberGuess == randomInt) { 
       System.out.println("Correct! You win!"); 
       System.out.println("It took you " + i + " guesses."); 
       System.exit(0); 
      } else if (NumberGuess < 0 || NumberGuess > 50) { 
       System.out.println("Invalid Input, please enter numbers between 0 and 50"); 
      } else if (NumberGuess < randomInt) { 
       System.out.println("Guess is too small."); 
      } else if (NumberGuess > randomInt) { 
       System.out.println("Guess is too big."); 
      } 
      System.out.println("You have made " + i + " guesses out of 7"); 
      i++; 
     } 

     System.out.println("Game over!"); 
     System.out.println("All 7 Guesses used!"); 
     System.exit(0); 
    } 
} 

而且你不應該Scanner UserGuess = new Scanner(System.in);使用內循環這一說法。 只能在循環外部創建一個掃描儀副本。