如何在循环结束后重复循环

时间:2016-10-26 04:28:30

标签: java while-loop count java.util.scanner generator

我正在做一个要求创建猜谜游戏但我已编码猜谜游戏的作业。我需要问用户他们是否想要再次玩猜谜游戏,我需要重新触发循环。我不确定如何在第一个循环之后重新触发,因为已经执行并完成了。

以下是我猜猜游戏的代码:

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!

从我的代码中的注释中可以看出,我尝试复制循环,同时还添加了if else语句和问题,询问用户是否要再次播放。有没有更容易和有效的方法来重新触发循环以使游戏再次开始?

提前谢谢

4 个答案:

答案 0 :(得分: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
            }
        }

答案 1 :(得分:1)

有两种方法,可以为整个程序添加一个while(true)循环,如果用户选择不再播放,或者使用函数,则会中断。

编辑:对于一个小程序,函数会过于复杂,只需这样做:

while(true){

//code here

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

答案 2 :(得分: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..
}

答案 3 :(得分:-1)

你可以用它来完全重新运行你的java程序:

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