2016-10-26 71 views
0

我正在做一個任務,要求創建一個猜謎遊戲,但我已經編寫了猜謎遊戲。我需要問用戶他們是否想再次玩猜謎遊戲,我需要重新觸發循環。我不知道如何在第一個循環之後重新觸發,因爲它已經被執行和完成。如何在循環完成後重複循環

這裏是我的猜謎遊戲代碼:

import java.util.Random; 
import java.util.Scanner; 
public class ThreeEleven 
{ 


public static void main (String[] args) 
    { 
final int MAX = 100; 
int answer, guess; 
String again; 
Scanner scan = new Scanner(System.in); 
System.out.print ("I'm thinking of a number between 1 and " 
+ MAX + ". Guess what it is: (or enter 0 to quit) "); 
guess = scan.nextInt(); 

Random generator = new Random(); //Random generator. 1 to 100. 
answer = generator.nextInt(MAX) +1; 

    //if (guess == answer){ //If guess equals answer 
     //System.out.println ("You got it! Good guessing!"); 

    //} 
    //else if (guess == 0){ //Game ends 
     //System.out.println ("You have ended your game. Goodbye."); 

    //} 

     while (guess != answer && guess != 0){ //If guess and 0 is not answer, continue. 

      if (guess > answer && guess != 0){ //If guess is higher than answer 
       System.out.println ("You guessed too high!"); 
       guess = scan.nextInt(); 

      } 
      else{ 
       if (guess < answer && guess != 0){ //If guess is lower than answer 
        System.out.println ("You guessed too low!"); 
        guess = scan.nextInt(); 
       } 

       else if (guess == answer){ //If guess equals answer 
        System.out.println ("You got it! Good guessing!"); 
       } 
       else if (guess == 0){ //Game ends 
        System.out.println ("You have ended your game. Goodbye."); 
       } 
        } 
} 
     if (guess == answer){ 
      System.out.println ("You got it! Good guessing!"); 
      //System.out.println ("You guessed " + + " times"); 

// System.out.println ("Want to play again? (yes or no)?"); 
     // again = scan.next(); 
     // if (again == "yes") 
    // { 
      // System.out.print ("I'm thinking of a number between 1 and " 


// + MAX + ". Guess what it is: (or enter 0 to quit) "); 
    //guess = scan.nextInt(); 
      // while (again == "yes") 
      // { 
      // while (guess != answer && guess != 0){ //If guess and 0 is not answer, continue. 

      // if (guess > answer && guess != 0){ //If guess is higher than answer 
       // System.out.println ("You guessed too high!"); 
       // guess = scan.nextInt(); 

      // } 
      // else{ 
      // if (guess < answer && guess != 0){ //If guess is lower than answer 
       //  System.out.println ("You guessed too low!"); 
       //  guess = scan.nextInt(); 
       // } 

      //  else if (guess == answer){ //If guess equals answer 
       // System.out.println ("You got it! Good guessing!"); 
     //  } 
     //  else if (guess == 0){ //Game ends 
        // System.out.println ("You have ended your game. Goodbye."); 
     //  } 
     //   } 
     //   } 
    // if (guess == answer){ 
      // System.out.println ("You got it! Good guessing!"); 
    // } 

     // System.out.println ("Want to play again? (yes or no)?"); 
     //again = scan.next(); 
    //} 
    //} 
     } 
} 

輸出:

I'm thinking of a number between 1 and 100. Guess what it is: (or enter 0 to quit) 50 
You guessed too high! 
40 
You guessed too high! 
30 
You guessed too high! 
20 
You guessed too high! 
10 
You guessed too high! 
1 
You guessed too low! 
5 
You got it! Good guessing! 

當你從我的代碼註釋中看到,我試圖複製循環而如果還添加else語句和一個問題來詢問用戶他們是否想要再次玩。有沒有更容易和更有效的方法來重新觸發循環,讓遊戲重新開始?

預先感謝您

回答

2

這裏是你必須做的:

boolean playAgain = true; 
     while(playAgain){ 
      //your game code here 
      System.out.println("Type yes to play again, No to exit"); 
      Scanner answer = new Scanner(System.in); 
      String s = answer.next();//this will take the answer of the user 
      answer.nextLine();//this will make sure it can take next input by returning new line 
      if(s.trim().toLowerCase().equals("yes")){ 
       playAgain = true;//if the player wants to play again, the loop will repeat 
      }else{ 
       playAgain = false;//else the loop will exit 
      } 
     } 
+0

我用你的代碼,它像是但是,當循環開始時,自動錶示我贏了,而不是讓我插入更多的數字。 –

+2

@Learning_Java這意味着你必須將你的重要變量重置爲初始值_side_'while(playAgain)'循環,即在'while(playAgain)'之後和遊戲代碼之前。 – ajb

+0

好吧,這是有道理的,謝謝大家的幫助! –

1

有兩種方式,要麼一會(true)循環添加到整個程序,如果用戶選擇不打再次突破,或使用功能。

編輯:函數將是一個小程序過於複雜,只是這樣做:

while(true){ 

//code here 

    if(notReplay){ //notReplay is a boolean that is true when player does not want to play again 
     break; 
    } 
} 
0

你可以做的就是把你的循環的另一循環內。例如:

boolean playAgain = true; 
while (playAgain) { 
    //Insert your main while loop here 
    //ask the user if he wants to play again after your main loop, and change playAgain variable accordingly.. 
} 
-1

,你可以用它來完全重新運行YOUT java程序:

try{ 
    Runtime.getRuntime().exec("java YOURFILENAME"); 
    System.exit(0); 
} catch (Exception exc){ 
    exc.printStackTrace(); 
} 
+0

即使它有效,這也是一個糟糕的解決方案。它可能不起作用,原因有很多。 – ajb

+0

@ajb這爲我工作。 –

+0

@ajb請告訴我爲什麼你認爲這是一個不好的解決方案 –