繁体   English   中英

Hi-Lo Guessing Game - 限制用户输入的尝试次数和再次播放逻辑

[英]Hi-Lo Guessing Game - Limiting number of attempts from user input & play again logic

我是 Java 编程和参加大学课程的新手,我的任务是创建一个 Hi/Lo 猜谜游戏。 游戏为用户提供最多 5 次尝试输入 1 到 100(含)之间的数字。 程序必须提供答案是否太低、太高或正确的逻辑反馈。 程序必须提供在获胜或 5 次尝试失败后重新玩的选项。

我已经重新创建了这个程序大约 10 次:(。我无法让他的逻辑按照上面的说明工作。我无法在 5 次尝试时停止尝试......而且我无法让程序执行新游戏。

任何帮助是极大的赞赏。 我花费了无数个小时来编写和重写这段代码,得到了许多不同的结果——但不是预期的结果。

这是我第一次发帖,如果发帖格式不正确,我深表歉意。

我浏览的论坛和示例比我愿意承认的要多,而且我审查并尝试实施的所有代码都没有给我带来将用户输入限制为每次尝试 5 次以及能够再次播放多次的结果。

这是我的代码:

public class HiLoGuessingGame {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //Initialize scanner and random number gennerator
        Scanner input = new Scanner(System.in); 
        Random generator = new Random();

        //State the rules of the game 
        System.out.println("The Hi-Lo Guessing Game. Guess a number between 1-100");
        System.out.println("You have 5 attempts!");

       /* define the variable Guess (user iput)
          define the variable Answer (random generator)
          define the variable Counter (track number of tries and limit to 5)
          define the variable PlayAgain (Y/N question)*/
        int guess = 0; 
        int answer = generator.nextInt(100)+1;
        int counter = 1;
        String playAgain;
        boolean gameOver = false;

        //Ask the Hi-Lo question - pick number 1-100 (inclusive) 
        //Provide feedback answer too high, too low or you win! 
        //Limit number of tries in the game to 5 

        while (guess != answer) {
            System.out.print("Enter your guess: ");
            guess = input.nextInt(); 
            counter++;

            if (guess < answer)  {
                 System.out.println("Your guess " + guess + " is too low. Try again");
                 System.out.println("This is attempt: " + counter);
            } else if (guess > answer) {
                 System.out.println("Your guess " + guess + " is too high. Try again");
                 System.out.println("This is attempt: " + counter);
            } else if (guess == answer) {
                 System.out.println("Your guess " + guess + " is correct! You win!");
                 System.out.println();
                 System.out.println("Would you like to play again (Y/N)?");
                 playAgain = input.next();
            } 

        }
        if (counter ==6) {
            System.out.println("Sorry, you've reached your max atttempts.");
            System.out.println("Would you like to play again (Y/N)?"); 
            playAgain = input.next();
        }

        // Play again logic
        boolean isValid;
        do {
            System.out.print("Would you like to play again (Y/N)?");
            playAgain = input.next().toUpperCase();
            isValid = playAgain.equals("Y") || playAgain.equals("N");
            playAgain = input.next();
            counter = 1;
            if ( !isValid ) {
                 System.out.println("Error, please enter Y or N");
                 System.out.println();
            }
        } while (!isValid);
    }
}

您可以在 while 循环中添加一个额外的条件:

while (guess != answer && counter < 5) {
    // ...
}

或者,您可以在得到正确答案时打破循环:

 while (counter < 5) {
    // ...
    if (answer == guess){
       // ...
       break;
    } 
 } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM