2014-06-08 35 views
-1

Java中的初學者在這裏。System.out.println - 語法錯誤

試圖找出爲什麼我得到最後兩個System.out.println語法錯誤。

我試着移動我的括號,但我最終得到了一個永無止境的循環。

我非常感謝您的幫助!


import java.util.Scanner; 

public class GuessingGame 
{ 

    public static void main(String args []) 
    { 
     Scanner scan = new Scanner (System.in); 

     int random = (int) (Math.random()*20 +1); 
     int numberOfAttempts = 0; 
     boolean win = false; 

     while (win == false) 

      System.out.println("Guess the number (1 - 20)"); 
      int guess = scan.nextInt(); 
      numberOfAttempts++; 

      if (guess == random) 
       win = true; 
      else if (guess < random) 
       System.out.println("Your guess was low. Guess again."); 
      else 
       System.out.println("Your guess was high. Guess again."); 
    } 

      System.out.println("You guessed right! It took you guesses."); 
      System.out.println("Do you want to play again? (yes or no)"); 

} 
+0

您應該正確縮進代碼。什麼被認爲是「while」循環的主體?投票關閉印刷錯誤。 –

+0

謝謝。我仍然試圖找出縮進問題。 while循環的主體應該是猜測遊戲執行的「猜數字」。 – CLTMedic

+0

1.在/ if/while/method/switch/class /等中使用大括號。 2.計算你的大括號,每個開口大括號應該有一個大括號 –

回答

1

你不開放while循環。此時需要括號

while (win == false){ 
//need one at the beginning 

     System.out.println("Guess the number (1 - 20)"); 
     int guess = scan.nextInt(); 
     numberOfAttempts++; 

     if (guess == random) 
      win = true; 
     else if (guess < random) 
      System.out.println("Your guess was low. Guess again."); 
     else 
      System.out.println("Your guess was high. Guess again."); 
} //and at the end 
+0

太棒了!非常感謝。僅在此Java課程的第3周!標記爲已解決。 – CLTMedic