2014-09-11 112 views
0

這是一個簡單的java猜數字遊戲。重新開始簡單的Java比賽

遊戲結束後,我想問用戶他是否想再玩一次:你想再玩一次嗎? (回答Y/N)如果用戶輸入Y,則遊戲將重新開始並隨機輸入一個新號碼,如果他回覆'N',則遊戲結束。這裏是我的代碼:

import static java.lang.Math.*; 
import java.util.Scanner; 


public class HomeWorkLoopGame1 { 
    public static void main(String[] args) { 
     System.out.print("Welcome to number guessing game !\n"); 
     System.out.println("Guess a number range from 1 to 100, You have 5 guess to win this game"); 
     int answer = (int) (random()*100); 
     Scanner guess1 = new Scanner (System.in); 
     System.out.println("To begin please input your number"); 
     int num1 = guess1.nextInt(); 
     if (num1<answer) { 
      System.out.print(num1); 
      System.out.print(" Your answer is too small\n"); 
      System.out.println("Try again! Please input your second guess");    
     } 
     else if (num1>answer){ 
      System.out.println(num1); 
      System.out.print("Your answer is too big\n"); 
      System.out.println("Try again! Please input your second guess"); 
     } 
     else { 
      System.out.println("Congratulation! Your answer is correct! You win !"); 
     } 

     int num2 = guess1.nextInt(); 
     if (num2<answer) { 
      System.out.print(num2); 
      System.out.print(" Your answer is too small\n"); 
      System.out.println("Try again! Please input your third guess"); 
     } 
     else if (num2>answer){ 
      System.out.println(num2); 
      System.out.print("Your answer is too big\n"); 
      System.out.println("Try again! Please input your third guess"); 
     } 
     else { 
      System.out.println("Congratulation! Your answer is correct! You win !"); 
     } 

     int num3 = guess1.nextInt(); 
     if (num3<answer) { 
      System.out.print(num3); 
      System.out.print(" Your answer is too small\n"); 
      System.out.println("Try again! Please input your fourth guess"); 
     } 
     else if (num3>answer){ 
      System.out.println(num3); 
      System.out.print("Your answer is too big\n"); 
      System.out.println("Try again! Please input your fourth guess"); 
     } 
     else { 
      System.out.println("Congratulation! Your answer is correct! You win !"); 
     } 

     int num4 = guess1.nextInt(); 
     if (num4<answer) { 
      System.out.print(num4); 
      System.out.print(" Your answer is too small\n"); 
      System.out.println("Try again! Please input your final guess"); 
     } 
     else if (num4>answer){ 
      System.out.println(num4); 
      System.out.print("Your answer is too big\n"); 
      System.out.println("Try again! Please input your final guess"); 
     } 
     else { 
      System.out.println("Congratulation! Your answer is correct! You win !"); 
     } 

     int num5= guess1.nextInt(); 
     if (num5<answer) { 
      System.out.print(num5); 
      System.out.print(" Your answer is too small\n"); 
      System.out.println("Game Over"); 
     } 
     else if (num5>answer){ 
      System.out.println(num5); 
      System.out.print("Your answer is too big\n"); 
      System.out.println("Game Over"); 
     } 
     else { 
      System.out.println("Congratulation! Your answer is correct! You win !"); 
     } 

     System.out.println("Correct Answer is "+ answer); 
     Scanner userReply = new Scanner (System.in); 
     char reply; 
     System.out.println("Do you want to play again? Answer with Y/N)"); 
    } 
} 
+3

您需要使用'do ... while'循環 – user902383 2014-09-11 10:04:59

+1

呃,冗餘太多。創建一個方法來接受一個整數,並將你的if-elseif-else登錄到那裏,並返回一個帶有響應的String來打印出來。 'System.out.println(checkGuess(guess1.nextInt()));' – 2014-09-11 10:05:12

+0

像@ user902383所提到的。 「儘管」就足夠了。您可能需要稍微清理一下代碼,以使其更易於閱讀。 – Bahamut 2014-09-11 10:12:21

回答

1

你可以嘗試這樣的事情。順便說一句,這不是一個好辦法。

Scanner userReply = new Scanner(System.in); 
System.out.println("Do you want to play again? Answer with Y/N)"); 
String answer= userReply.nextLine().toLowerCase(); 
    if("y".equals(answer)){ 
     main(args); // recursively call 
    }else { 
     System.out.println("game exit"); 
    } 

您可以使用do-while以正確的方式做到這一點。

更好的辦法

public static void main(String[] args) { 
    boolean isValid; 
    do { 
     isValid = true; 
     runGame(); 
     Scanner userReply = new Scanner(System.in); 
     System.out.println("Do you want to play again? Answer with (Y/N)"); 
     String answer = userReply.nextLine().toLowerCase(); 
     if ("y".equals(answer)) { 
      isValid =true; 
     } else { 
      isValid =false; 
      System.out.println("game exit"); 
     } 
    } while (isValid); 

    } 

public static void runGame() { 
    // your game logic 
} 
+0

感謝您回覆我的問題兄弟! – 2014-09-11 10:13:55

+0

但是當我嘗試把你的代碼和答案與y遊戲不重新啓動它只是說遊戲出口:( – 2014-09-11 10:14:45

+0

這是一個壞主意,遞歸有它的地方,但它絕對不適合這個或新的程序員只是 – 2014-09-11 10:18:30

0

可以調用該類的主要方法,並創建一個類的實例作爲如此:

HomeWorkLoopGame1 variable = new HomeWorkLoopGame1(); 
HomeWorkLoopGame1.main(args); 

基本上,它從頭再次運行遊戲 - 作爲如果玩家第一次開始再次玩遊戲。

這將是這樣的:

Scannner in = new Scanner(System.in); 
System.out.println("Do you want to play again? Answer with Y/N"); 
String answer = in.nextLine().toLowerCase(); 
if(answer.equals.("Y")){ 
    HomeWorkLoopGame1 variable = new HomeWorkLoopGame1(); 
    HomeWorkLoopGame1.main(args); 
} 
else{ 
    //exit 
} 

但是我還沒有測試,所以我不是100%肯定它可能工作。好運

編輯: - 換行代碼在for循環中重複了無數次,而不僅僅是一次

0

最好是(我認爲),以便爲播放功能的方法。

public static void main(String[] args) { 
    playGame(); 
    System.out.print("Enter something:"); 

    String input = System.console().readLine(); 
    if (input=="y"){ 
     playGame(); 
    } else { 
     System.exit(0); 
    } 
} 

private void playGame() { 
    System.out.print("Welcome to number guessing game !\n"); 
    System.out.println("Guess a number range from 1 to 100, You have 5 guess to win this game"); 
    int answer = (int) (random()*100); 
    Scanner guess1 = new Scanner (System.in); 
.... 
    System.out.println("Correct Answer is "+ answer); 
    Scanner userReply = new Scanner (System.in); 
    char reply; 
    System.out.println("Do you want to play again? Answer with Y/N)"); 

} 
0

只要在您的主要方法中放置一個do-while循環。

作爲第一行認沽:

do { 

緊接着System.out.println("Do you want to play again? Answer with Y/N)");地說:

while (reply=='Y' or reply=='y'); 

你需要確保你在回覆正常閱讀等課程。

0

你可以做整個事情在一個循環:

public static void main(String[] args) { 
    while (true) { 

     // your code goes here 

     System.out.println("Do you want to play again? Answer with Y/N)"); 

     try { 
      char reply; 
      reply = (char) System.in.read(); // read a char 
      if (Character.toLowerCase(reply) != 'y') // when equals to Y break the loop 
       break; 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 
0

我試着不要改變太多的原密碼。我添加了幾個while循環,一些註釋和額外的代碼來處理用戶的響應。我希望它有幫助。

import static java.lang.Math.*; 
import java.util.Scanner; 

public class Test { 
    public static void main(String[] args) { 
     boolean play = true; 
     boolean notCorrect = true; 

     while (play) { 
      while (notCorrect) { 
       System.out.print("Welcome to number guessing game !\n"); 
       System.out.println("Guess a number range from 1 to 100, You have 5 guess to win this game"); 

       int answer = (int) (random() * 100); 
       Scanner guess1 = new Scanner(System.in); 

       //Guess 1 
       System.out.println("To begin please input your number"); 
       int num1 = guess1.nextInt(); 
       if (num1 < answer) { 
        System.out.print(num1); 
        System.out.print(" Your answer is too small\n"); 
        System.out.println("Try again! Please input your second guess"); 
       } else if (num1 > answer) { 
        System.out.println(num1); 
        System.out.print("Your answer is too big\n"); 
        System.out.println("Try again! Please input your second guess"); 
       } else { 
        System.out.println("Congratulation! Your answer is correct! You win !"); 
        break; 
       } 

       //Guess 2 
       int num2 = guess1.nextInt(); 
       if (num2 < answer) { 
        System.out.print(num2); 
        System.out.print(" Your answer is too small\n"); 
        System.out.println("Try again! Please input your third guess"); 
       } else if (num2 > answer) { 
        System.out.println(num2); 
        System.out.print("Your answer is too big\n"); 
        System.out.println("Try again! Please input your third guess"); 
       } else { 
        System.out.println("Congratulation! Your answer is correct! You win !"); 
        break; 
       } 

       //Guess 3 
       int num3 = guess1.nextInt(); 
       if (num3 < answer) { 
        System.out.print(num3); 
        System.out.print(" Your answer is too small\n"); 
        System.out.println("Try again! Please input your fourth guess"); 
       } else if (num3 > answer) { 
        System.out.println(num3); 
        System.out.print("Your answer is too big\n"); 
        System.out.println("Try again! Please input your fourth guess"); 
       } else { 
        System.out.println("Congratulation! Your answer is correct! You win !"); 
        break; 
       } 

       //Guess 4 
       int num4 = guess1.nextInt(); 
       if (num4 < answer) { 
        System.out.print(num4); 
        System.out.print(" Your answer is too small\n"); 
        System.out.println("Try again! Please input your final guess"); 
       } else if (num4 > answer) { 
        System.out.println(num4); 
        System.out.print("Your answer is too big\n"); 
        System.out.println("Try again! Please input your final guess"); 
       } else { 
        System.out.println("Congratulation! Your answer is correct! You win !"); 
        break; 
       } 

       //Guess 5 
       int num5 = guess1.nextInt(); 
       if (num5 < answer) { 
        System.out.print(num5); 
        System.out.print(" Your answer is too small\n"); 
        System.out.println("Game Over"); 
       } else if (num5 > answer) { 
        System.out.println(num5); 
        System.out.print("Your answer is too big\n"); 
        System.out.println("Game Over"); 
       } else { 
        System.out.println("Congratulation! Your answer is correct! You win !"); 
        break; 
       } 

       System.out.println("Correct Answer is " + answer); 
       break; 

      } // while(correct) 

      Scanner userReply = new Scanner(System.in); 
      String reply; 
      System.out.println("Do you want to play again? Answer with (Y/N)"); 
      reply = userReply.next().toLowerCase(); 
      if (reply.equals("y")) { 
       play = true; 
      } else if (reply.equals("n")) { 
       play = false; 
      } else { 
       System.out.println("Invalid choice."); 
       break; 
      } 

     } // while(play) 
    } // main() 
} // class Test