2013-11-04 29 views
0

/你好,我正在嘗試學習如何在java中使用「break」commend以及用「y或n」選項繼續循環。我正在寫這個遊戲猜數字,我在選擇「y」時遇到了一些麻煩。我會試着解釋一下,編寫一個猜數字的遊戲很容易,所以我開始添加一些條件,如可能性和再次玩或不玩,後來我認爲如果增加退出任何可能性會更有趣玩家希望的時間,但這不能正常工作。請幫助,這就是我的代碼關於猜測的非常簡單的遊戲

package guessinggame; 
import java.util.Random; 
import java.util.Scanner; 

/** 
* 

public class Main { 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 
    System.out.println(" Welcome "); 

    Random rand = new Random(); 
    Scanner input = new Scanner(System.in); 

    boolean play_again = true; 
    while (play_again) 
    { 
    int number_guess = rand.nextInt(100)+1; 
    int number_of_tries = 0; 
    int guess; 
    String another = "y"; 

    boolean win = false; 
    while (win == false) 
    { 
     System.out.println(" Try too guess a number between 1 and 100 "); 
     guess = input.nextInt(); 
     number_of_tries++; 

     if (guess == number_guess) 
     { 
      win = true; 
     } 
     else if (guess < number_guess) 
     { 
      System.out.println(" Guess is too low " + "\n Guess another number to continue or n to quit "); 
      if (input.hasNext("n")) 
      { 
       play_again = false; 
       break; 

      } 
     } 
     else if (guess > number_guess) 
     { 
      System.out.println(" Guess is too high " + "\n Guess another number to continue or n to quit "); 
      if (input.hasNext("n")) 
      { 
       play_again = false; 
       break; 
      } 
     } 
    } 

    System.out.println(" You Win!!! "); 
    System.out.println(" The number was " + number_guess); 
    System.out.println(" It took you " + number_of_tries + " tries " + 
      "\nWould you like to play again? (y/n): "); 
    if (another.equalsIgnoreCase("y") == true) 
     play_again = true; 
    else 
    { 
     play_again = false; 
     } 
    } 
    } 
} 
+2

不是真的關係到你的問題,但你應該看看[Java命名約定(http://www.oracle.com/technetwork/java/codeconventions-135099.html#367) – redFIVE

+0

如果我看到它,你的代碼打印「你贏了」,當你中止循環。使用'return'而不是break來立即退出。 – PMF

+0

考慮粘貼代碼片段而不是整個班級。在問這裏之前,最好自己縮小問題的範圍;這樣你就會知道哪幾行代碼是罪魁禍首。 –

回答

2

這裏是實現了相同的結果

public class Main { 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) 
{ 
    System.out.println(" Welcome "); 

    Random rand = new Random(); 
    Scanner input = new Scanner(System.in); 

    int guess = 0; 
    int number_of_tries = 0; 

    System.out.println(" Try too guess a number between 1 and 100 -1 to close"); 
    guess = input.nextInt(); //get first input 
    while (guess != -1) 
    { 
     int number_guess = rand.nextInt(5) + 1; 
     ++number_of_tries; 

     //check if user wins and exits loop 
     if (isWin (number_guess,guess)) 
     { 
      System.out.println(" You Win!!! "); 
      System.out.println(" The number was " + number_guess); 
      System.out.println(" It took you " + number_of_tries + " tries " + 
        "\nWould you like to play again? [1 yes/ -1 no]: "); 
      guess = input.nextInt(); 
      if (guess == -1) 
      break; 

      else 
       System.out.println(" Try too guess a number between 1 and 100 -1 to close"); 
     } 
     else if (number_guess < guess) 
     { 
      System.out.println(" Guess is too High " + "\n Guess another number to continue or -1 to quit "); 
      guess = input.nextInt(); 
      continue; 
     } 
     else if (number_guess > guess) 
     { 
      System.out.println(" Guess is too low " + "\n Guess another number to continue or -1 to quit "); 
      guess = input.nextInt(); 
      continue; 
     } 

    } 
    System.out.println ("bye bye"); 
} 

public static boolean isWin (int number,int guess) 
{ 
    return (number == guess) ? true :false; 
} 

}

替代
+0

謝謝你,愛你的想法,真棒的幫助,歡呼:)我改變 - 1與0(零),並工作得很好。愛Java編程,你可以在很多方面做一些事情。 – user2900919

1

你忘了這個說法後,等待用戶輸入:

System.out.println(" It took you " + number_of_tries + " tries " + 
     "\nWould you like to play again? (y/n): "); 

例如你可以嘗試下一個方法:

System.out.println(" It took you " + number_of_tries + " tries " + 
     "\nWould you like to play again? (y/n): "); 
if (input.hasNext()) { 
    if (another.equalsIgnoreCase("y")) { 
    play_again = true; 
    input.next(); 
    } else { 
    play_again = false; 
    } 
} 
+0

謝謝,我想我添加了用戶輸入,但是如果我選擇「y」不起作用。 – user2900919

+0

@ user2900919,查看更新 – bsiamionau

+0

謝謝,我忘了那個,真棒幫助,歡呼聲:) – user2900919

0

它看起來像你的括號,如果搞砸了你的發揮再次聲明

0

這將起作用。

package aa; 
    import java.util.Random; 
    import java.util.Scanner; 


    public class abc { 


     public static void main(String[] args) { 
      System.out.println(" Welcome "); 

      Random rand = new Random(); 
      Scanner input = new Scanner(System.in); 

      boolean play_again = true; 
      while (play_again) 
      { 
       int number_guess = rand.nextInt(100)+1; 
       int number_of_tries = 0; 
       int guess; 
       String another = "y"; 

       boolean win = false; 

       while (win == false) 
       { 
        System.out.println(" Try too guess a number between 1 and 100 "); 
        guess = input.nextInt(); 
        number_of_tries++; 

        if (guess == number_guess) 
        { 
         win = true; 
         break; 
        } 
        else if (guess < number_guess) 
        { 
         System.out.println(" Guess is too low " + "\n Guess another number to continue or n to quit "); 
         if (input.hasNext("n")) 
         { 
          play_again = false; 
          break; 

         } 
        } 
        else if (guess > number_guess) 
        { 
         System.out.println(" Guess is too high " + "\n Guess another number to continue or n to quit "); 
         if (input.hasNext("n")) 
         { 
          play_again = false; 
          break; 
         } 
        } 
       } 

       input.next(); 

       if (win == true){ 
        System.out.println(" You Win!!! "); 
       } 
       else{ 
        System.out.println(" Good Luck Next Time!!! "); 
        System.out.println(" The number was " + number_guess); 
       } 

       System.out.println(" It took you " + number_of_tries + " tries " + 
         "\nWould you like to play again? (y/n): "); 

       another = input.next(); 
       if (another.equalsIgnoreCase("y") == true) 
        play_again = true; 
       else 
       { 
        play_again = false; 
       } 
      } 
      System.out.println("Thank you!!!"); 
     } 

    } 
+0

實際上應該嘗試和解釋的東西,而不是隻是傾銷的答案 – redFIVE

+0

謝謝你,我喜歡你的想法解決這個問題,一點點改變和完美的作品,歡呼 – user2900919

+0

要求在這裏沒有幫助你的代碼更正。學習調試你的代碼。祝一切順利。 –