如何重播基本的高低游戏?

时间:2015-08-31 02:01:58

标签: java

我几乎完成了第二次Java课程的分配,但我仍然坚持如何采取用户的最后选择,或者再次播放或退出。有什么想法吗?

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

    public static void main(String[]args) {

        Random randomNumber = new Random();
        int answer = randomNumber.nextInt(100) + 1;
        int numberOfTries = 0;
        Scanner userInput = new Scanner(System.in);
        String userGuess = null;
        boolean gameWin = false;

        do {
            while (!gameWin) {
                System.out.println("Welcome to the guessing game, guess a number between 1 - 100 or type 'quit' to exit: ");
                userGuess = userInput.nextLine();
                numberOfTries++;


                if (userGuess.equals("quit"))
                    break;
                if (Integer.parseInt(userGuess) == answer) {
                    gameWin = true;
                } else if (Integer.parseInt(userGuess) < answer) {
                    System.out.println("Your guess is too low");
                } else if (Integer.parseInt(userGuess) > answer) {
                    System.out.println("Your guess is too high");
                }

            }

            if (userGuess.equals("quit")) {
                System.out.println("You choose to quit! Thanks for playing");
                System.out.println("The number was " + answer);
                System.exit(0);
            }
            System.out.println("The number was " + answer);
            System.out.println("It took you " + numberOfTries + " tries");
            System.out.println("You win! Play again(type: yes or no)");


        }
        while (userGuess.equals("yes"));
            userInput.nextLine();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

问题是因为您有userInput.nextLine()提示用户再次播放或不在游戏循环之外播放,并且未将其分配给任何值。我指的是这一节......

        System.out.println("The number was " + answer);
        System.out.println("It took you " + numberOfTries + " tries");
        System.out.println("You win! Play again(type: yes or no)");


    }
    while (userGuess.equals("yes"));
        userInput.nextLine();  <---
    }

要解决此问题,您只需将最终输入调用分配给userGuess,然后将其重新置于游戏循环中。

        System.out.println("The number was " + answer);
        System.out.println("It took you " + numberOfTries + " tries");
        System.out.println("You win! Play again(type: yes or no)");

        userGuess = userInput.nextLine();
    }
    while (userGuess.equals("yes"));
    }

执行此操作会将您提示用户的play again值分配到使用do-while循环进行测试的变量中。在您的原始版本中,您已经测试了userGuess不会存储的值的条件,而是在循环退出后而不是内部需要的位置时提示用户输入该选项。 / p>

要重置下一个游戏的值,你可以简单地将变量的初始化移动到顶部的游戏循环中,这样......

import java.util.Random;

import java.util.Scanner; 公共课HighLow {

public static void main(String[]args) {

    Random randomNumber = new Random();
    int numberOfTries, answer;
    Scanner userInput = new Scanner(System.in);
    String userGuess;
    boolean gameWin;

    do {
        answer = randomNumber.nextInt(100) + 1;
        gameWin = false;
        userGuess = null;
        numberOfTries = 0;

        while (!gameWin) {
            System.out.println("Welcome to the guessing game, guess a number between 1 - 100 or type 'quit' to exit: ");
            userGuess = userInput.nextLine();
            numberOfTries++;


            if (userGuess.equals("quit"))
                break;
            if (Integer.parseInt(userGuess) == answer) {
                gameWin = true;
            } else if (Integer.parseInt(userGuess) < answer) {
                System.out.println("Your guess is too low");
            } else if (Integer.parseInt(userGuess) > answer) {
                System.out.println("Your guess is too high");
            }

        }

        if (userGuess.equals("quit")) {
            System.out.println("You choose to quit! Thanks for playing");
            System.out.println("The number was " + answer);
            System.exit(0);
        }
        System.out.println("The number was " + answer);
        System.out.println("It took you " + numberOfTries + " tries");
        System.out.println("You win! Play again(type: yes or no)");

        userGuess = userInput.nextLine();
    }
    while (userGuess.equals("yes"));

    }
}
}

这种方式每次游戏循环播放时,所有值都将在新游戏循环开始时重置。

此外,您应该在每个实例上使用Random关闭整个程序末尾的Scanner对象和close()对象。

答案 1 :(得分:0)

您需要初始化大部分变量并在外部循环中调用userInput.nextLine();。未经测试的建议:

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

    public static void main(String[]args) {

        String userGuess = null;

        do {
            Random randomNumber = new Random();
            int answer = randomNumber.nextInt(100) + 1;
            int numberOfTries = 0;
            Scanner userInput = new Scanner(System.in);
            boolean gameWin = false;
            while (!gameWin) {
                System.out.println("Welcome to the guessing game, guess a number between 1 - 100 or type 'quit' to exit: ");
                userGuess = userInput.nextLine();
                numberOfTries++;


                if (userGuess.equals("quit"))
                    break;
                if (Integer.parseInt(userGuess) == answer) {
                    gameWin = true;
                } else if (Integer.parseInt(userGuess) < answer) {
                    System.out.println("Your guess is too low");
                } else if (Integer.parseInt(userGuess) > answer) {
                    System.out.println("Your guess is too high");
                }

            }

            if (userGuess.equals("quit")) {
                System.out.println("You choose to quit! Thanks for playing");
                System.out.println("The number was " + answer);
                System.exit(0);
            }
            System.out.println("The number was " + answer);
            System.out.println("It took you " + numberOfTries + " tries");
            System.out.println("You win! Play again(type: yes or no)");


        }
        userInput.nextLine();
        while (userGuess.equals("yes"));
        }
    }
}