2014-09-24 98 views
0

好吧,所以我嘗試了一種方法,但每次輸入數字時都會顯示「你想再玩一次」。這是我到目前爲止。我還能做些什麼,以便只有在用戶猜到正確的答案後纔會問他/她是否想再玩一次?猜測遊戲再次出現問題

import java.util.Random; 
import java.util.Scanner; 

public class GuessNumber { 

    public static void main(String[] args) { 

     Scanner scan = new Scanner(System.in); 

     Random rand = new Random(); 
     int number = rand.nextInt(100) + 1; 
     int guess; 

     System.out.println("Guess the number between 1 and 100\n"); 

     guess = scan.nextInt(); 

     while (true) { 
      if(guess < number)  
       System.out.println("Higher!"); 
      else if(guess > number) 
       System.out.println("Lower!"); 
      else if (guess == number){ 
       System.out.println("Correct!"); 
      } 
       guess = scan.nextInt(); 
     } 
    } 
    } 

回答

0

您需要在正確時退出while。

要做到這一點,該行之後:

System.out.println("Correct!"); 

添加另一條線路是這樣的:

break; 
0

大多是出於好玩,我實現了一個遊戲適合你試圖什麼說明去做。代碼不是最乾淨/最有效的,但它絕對有效!

看看它,看看你是否能找出你的問題。

import java.util.Random; 
import java.util.Scanner; 

public class GuessingGame { 

public static void main(String[] args) { 

     System.out.println("Guess the number between 1 and 100"); 

     playGuessingGame(); 

     while (true) { 
      System.out 
       .println("Do you want to play again? Type y/n and hit enter"); 

      Scanner scan = new Scanner(System.in); 
      String playAgain = scan.next(); 

      if (playAgain.charAt(0) == 'y') { 
       System.out.println("Okay, I picked a new number. Good luck!"); 
       playGuessingGame(); 
      } else { 
       System.out.println("Thanks for playing!"); 
       break; 
      } 
     } 

    } 

    public static void playGuessingGame() { 
     Scanner scan = new Scanner(System.in); 
     Random rand = new Random(); 

     int guess; 
     int number = rand.nextInt(100) + 1; 

     guess = scan.nextInt(); 

     while (guess != number) { 
      if (guess < number) 
       System.out.println("Higher!"); 
      else if (guess > number) 
       System.out.println("Lower!"); 
      else if (guess == number) { 
       System.out.println("Correct!"); 
      } 
      guess = scan.nextInt(); 
     } 
    } 
} 
1

嘗試這個

class GuessNumber { 

    static Random rand = new Random(); 
    static Scanner scan = new Scanner(System.in); 
    static int number; 

    public static void main(String[] args) { 
     playGame(); 
    } 

    public static void playGame() { 
     number = rand.nextInt(100) + 1; 
     System.out.println("Guess the number between 1 and 100"); 
     while (true) { 
      int guess = scan.nextInt(); 
      if (guess < number) { 
       System.out.println("Higher!"); 
      } else if (guess > number) { 
       System.out.println("Lower!"); 
      } else if (guess == number) { 
       System.out.println("Correct!"); 
       System.out.println("Do you like to play again?[1 for Yes/0 for No]"); 
       int val = scan2.next(); 
       if (val == 1) 
        playGame(); 
       else 
        break; 
      } 
     } 
    } 
} 

或而不是使用相同的掃描儀,你可以使用另一個掃描儀,並得到字符串輸入如下

else if (guess == number) { 
     System.out.println("Correct!"); 
     Scanner scan2 = new Scanner(System.in); 
     System.out.println("Do you like to play again?[Y/N]"); 
     String val = scan2.next(); 
     if (val.equalsIgnoreCase("Y")) 
      playGame(); 
     else 
      break; 
} 
+0

這是一個好!但我刪除了System.out.println(number);所以它會在運行時顯示數字 – user4068770 2014-09-24 05:30:35

+0

@ user4068770抱歉有錯誤。我只是爲了我的調試目的。在發佈答案之前,我忘了刪除它。現在我刪除它。希望這對你有所幫助。 – 2014-09-24 05:34:14

+0

幫了很多,謝謝! – user4068770 2014-09-24 06:15:58