重启计划/游戏

时间:2016-03-10 16:11:49

标签: java loops while-loop

我正在研究这种随机数猜谜游戏。在游戏结束时,我希望用户可以选择再次播放(或让其他人玩)。我发现了几个类似的线索和问题,但没有一个能够帮助我解决这个小问题。我很确定我可以使用我的while循环,但不确切知道如何...

Java非常新,所以要了解这是否很容易解决..

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

public class GuessingGame {
public static void main(String[] args){

Random rand = new Random();
int correctNumber = rand.nextInt(100);
int numberOfGuesses = 0;
Scanner input = new Scanner(System.in);
int guess;
boolean win = false;    
String username = "";

    System.out.println("Hello, it is time to play a guessing game.");
    System.out.println("We will generate a random number between 0-99, and you will have to try to guess the number in as few attempts as possible.");
    System.out.println("You can also choose to give up by pressing \"q\" on your keyboard. \n\nTo get started, press \"s\" on your keyboard.");
    input.nextLine();

    System.out.println("\nEnter a username: ");
    username = input.nextLine();
    System.out.println("\nNew username registered: " + username);


    while(win == false){

    System.out.println("\nGuess a number between 0-99: ");
    guess = input.nextInt();
    numberOfGuesses++;

    if (guess == correctNumber){
        win = true;
    }
     else if(guess < correctNumber){
        System.out.println("Too low. Try again!");   
    }else if(guess > correctNumber){
        System.out.println("Too high. Try again!");   
    }
    }

    System.out.println("\nYou guessed the correct number which was " + correctNumber + ". Congrats!");
    System.out.println("\n" + username + " your number of guesses were: " + numberOfGuesses);

    win = false;
        }

 //     System.out.println("\nTo play again (let someone else try) press      \"s\", to quit press \"q\".");
 //     input.nextLine();
  //    if (input.next().equalsIgnoreCase("q")){
 //     System.out.println("\nThanks for playing!");
 //     }
 //     }else if(input.nextLine().equalsIgnoreCase("s")){       

    }

 //}    

2 个答案:

答案 0 :(得分:1)

您所描述的内容称为游戏循环&#34;。基本上你将整个游戏包裹在一个循环中,这个循环会根据某些条件重复。在这种情况下,条件是用户是否想再次播放。

在Java-ish伪代码中,结构如下所示:

boolean keepPlaying = true;
while (keepPlaying) {

    boolean hasWon = false;
    while (!hasWon) {
        // play a round in the game
    }

    keepPlaying = promptUser("Would you like to play again?");
}

&#34;伪&#34;伪代码的一部分当然是最后一行。只需询问用户是否想再次播放,并将响应转换为游戏循环使用的逻辑值。

所以游戏本身就是一轮#34;轮次&#34;重复直至输赢。 程序是一个&#34;游戏&#34;重复,直到用户退出。

答案 1 :(得分:0)

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

    public class GuessingGame {
       public static void main(String[] args){

       Random rand = new Random();
       int correctNumber = rand.nextInt(100);
       int numberOfGuesses = 0;
       Scanner input = new Scanner(System.in);
       int guess;
       boolean win = false;    
       String username = "";
       String choice = "No"; //new Variable

System.out.println("Hello, it is time to play a guessing game.");
System.out.println("We will generate a random number between 0-99, and you    will have to try to guess the number in as few attempts as possible.");
System.out.println("You can also choose to give up by pressing \"q\" on your keyboard. \n\nTo get started, press \"s\" on your keyboard.");
input.nextLine();

System.out.println("\nEnter a username: ");
username = input.nextLine();
System.out.println("\nNew username registered: " + username);

do{
while(win == false){

System.out.println("\nGuess a number between 0-99: ");
guess = input.nextInt();
numberOfGuesses++;

if (guess == correctNumber){
    win = true;
}
 else if(guess < correctNumber){
    System.out.println("Too low. Try again!");   
}else if(guess > correctNumber){
    System.out.println("Too high. Try again!");   
}
}

System.out.println("\nYou guessed the correct number which was " + correctNumber + ". Congrats!");
System.out.println("\n" + username + " your number of guesses were: " + numberOfGuesses);

win = false;
//added the next two lines
System.out.println("Do you want to play again?  Type \'Yes\' to play again or \'No' to quit");
choice = input.next();
}

     while(choice.equalsIgnoreCase("Yes"));

    }

 //     System.out.println("\nTo play again (let someone else try) press 
 \"s\", to quit press \"q\".");
   //     input.nextLine();
    //    if (input.next().equalsIgnoreCase("q")){
     //     System.out.println("\nThanks for playing!");
     //     }
     //     }else if(input.nextLine().equalsIgnoreCase("s")){       

  }

 //}