从用户输入结束并重新启动程序

时间:2016-04-20 04:47:58

标签: java

我遇到了一些问题,

  1. 我遇到问题让我的程序真正结束。
  2. 当我提示用户再次播放如何发起新游戏时,我无法弄明白。
  3. 任何指导都将不胜感激。

    由于

    import java.util.*;
    
    public class HiLowGuessingGame {
    
    
    public static void main(String[] args) {
    
        // Creates a random number generator
        Random random= new Random();
        // For getting input from the user
        Scanner sc = new Scanner(System.in);
    
        int number = random.nextInt(100);
        int guess = +1;
    
        //Counts the number of guesses
        int guesscount = 0;
        guesscount ++;
    
        //Allows user to quit the game
        String input;
        int quit = 0;
    
        String playagain;
            String y="y";
            String n="n";
    
    
    
        // Prompts user for their next guess
        System.out.println("Enter a guess from 1 to 100, Enter 0 to quit");         
    
    
        // Loop until the user guesses correctly
        while (true){ 
        // Reads the next guess
            guess = sc.nextInt();
        //If guess it to low or high
            if (guess == quit){
            System.out.println("Game Over!");}
            else if (guess < number ){
            System.out.println("Guess is too low, please try again");
            guesscount ++; }
            else if(guess > number ){
            System.out.println("Guess is too high, please try again");
            guesscount ++; }
    
            // Correct guess and number of guesses
            else {
            System.out.println("Your guess is correct = " + number + "\n" +"Number of Guesses = " +guesscount );
    
            while (true) {
                 // Play again?
                boolean isplayagain = true;
                while (true) {
                    System.out.println("play another game?(y/n)");
                    String playagainResponse = sc.next();
                    if (playagainResponse.equalsIgnoreCase("y")) {
                        System.out.println("Enter a guess from 1 to 100, Enter 0 to quit");
                        guess = sc.nextInt();
                        break;
                    } else if (playagainResponse.equalsIgnoreCase("n")) {
                        System.out.println("Goodbye");
                        isplayagain = false;
                        break;
                    }
                }
                if (!isplayagain) {
                    break;
                    }
                }
            }
            }
        }
    }
    

2 个答案:

答案 0 :(得分:0)

我运行了你的代码,我注意到你使用break语句退出for循环,但是你永远不会脱离最外层的for循环,所以游戏实际上永远不会结束。

在这种情况下,我建议不要使用break并使用System.exit(0),这将正常结束游戏。我将在底部添加一个参考链接。这应该可以解决你的游戏问题没有结束的问题。

至于你的其他问题,我不确定你的意思。你是说它没有正确重启新游戏吗?随机数仅在程序开始时发出,因此当新游戏开始时,随机数保持不变。此外,新游戏不会重置猜测计数。我只是将所有重置变量的代码放在if语句中,负责启动一个新游戏。

祝你好运!

https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#exit(int)

答案 1 :(得分:0)

我改变了你的程序以做你想做的事。 如果我得到你想要做的事情,我认为这就是代码。

import java.util.*;

public class HiLowGuessingGame {

public static void main(String[] args) {

    // Creates a random number generator
    Random random= new Random();
    // For getting input from the user
    Scanner sc = new Scanner(System.in);

    int number = random.nextInt(100);
    int guess = +1;

    //Counts the number of guesses
    int guesscount = 0;
    guesscount ++;

    //Allows user to quit the game
    String input;
    int quit = 0;

    String playagain;
        String y="y";
        String n="n";

        boolean isplayagain = false;

    // Prompts user for their next guess
    System.out.println("Enter a guess from 1 to 100, Enter 0 to quit");         


    // Loop until the user guesses correctly
    while (true){ 
    // Reads the next guess
        guess = sc.nextInt();
    //If guess it to low or high
        if (guess == quit){
        System.out.println("Game Over!");
        break;}

        else if (guess < number ){
        System.out.println("Guess is too low, please try again");
        guesscount ++; }
        else if(guess > number ){
        System.out.println("Guess is too high, please try again");
        guesscount ++; }

        // Correct guess and number of guesses
        else {
        System.out.println("Your guess is correct = " + number + "\n" +"Number of Guesses = " +guesscount );

       // while (true) {
             // Play again?

            while (true) {
                System.out.println("play another game?(y/n)");
                String playagainResponse = sc.next();
                if (playagainResponse.equalsIgnoreCase("y")) {
                    System.out.println("New game starts now!");                   
                    isplayagain=true;
                    break;
                } else if (playagainResponse.equalsIgnoreCase("n")) {
                    System.out.println("Goodbye");
                    isplayagain = false;
                    break;
                }
            }
            if (!isplayagain)break;//cause of the program to not end if user gives n
        }
        if(isplayagain){
            number = random.nextInt(100);
            guesscount=0;                       
            System.out.println("Enter a guess from 1 to 100, Enter 0 to quit");
            isplayagain=false;}

        }
    }
}