简体   繁体   English

为什么我的 java 猜谜游戏中的 changeDifficulty while 循环不重复?

[英]Why is the changeDifficulty while loop in my java guessing game not repeating?

I am a beginner programmer and I made this number guessing game which generates a random number (the range depending on the selected difficulty).我是一名初级程序员,我制作了这个猜数字游戏,它会生成一个随机数(范围取决于所选难度)。 I am trying to make it so that the user is able to reselect their difficulty if they'd like to change their selection.我正在努力使用户能够重新选择他们的难度,如果他们想改变他们的选择。 I used the while (changeDifficulty == true) to try and do this but when running the code no matter the user input the loop does not repeat.我使用 while (changeDifficulty == true) 尝试执行此操作,但是无论用户输入如何运行代码,循环都不会重复。 Any suggestions?有什么建议? Thanks.谢谢。

    //Create a random number for the user to guess
    int theNumber = 0;
    Boolean changeDifficulty = true;



        do {

            while (changeDifficulty == true) {

                System.out.println("Welcome to the number guessing game! \nSelect your difficulty (easy, medium, or hard.)");
                String difficulty = "";
                difficulty = scan.nextLine();

                if (difficulty.equalsIgnoreCase("easy")) {
                    theNumber = (int)(Math.random() * 100 + 1);
                } else if (difficulty.equalsIgnoreCase("medium")) {
                    theNumber = (int)(Math.random() * 1000 + 1);
                } else if (difficulty.equalsIgnoreCase("hard")) {
                    theNumber = (int)(Math.random() * 10000 + 1); 
                }

                String correctDifficulty = "";

                if (difficulty.equalsIgnoreCase("easy")) {
                    System.out.println("You have selected " + difficulty + " meaning you must guess a number from 1-100"
                            + " is this okay? (y/n)");
                    correctDifficulty = scan.nextLine();
                    if (correctDifficulty.equalsIgnoreCase("n")) {
                        changeDifficulty = false;
                    }
                } else if (difficulty.equalsIgnoreCase("medium")) {
                    System.out.println("You have selected " + difficulty + " meaning you must guess a number from 1-1000"
                            + " is this okay? (y/n)");
                    correctDifficulty = scan.nextLine();
                    if (correctDifficulty.equalsIgnoreCase("n")) {
                        changeDifficulty = false;
                    }
                } else if (difficulty.equalsIgnoreCase("hard")) {
                    System.out.println("You have selected " + difficulty + " meaning you must guess a number from 1-10000"
                            + " is this okay? (y/n)");
                    correctDifficulty = scan.nextLine(); 
                    if (correctDifficulty.equalsIgnoreCase("n")) {
                        changeDifficulty = false;
                    }
                }

            }
        System.out.println(theNumber);
        int guess = 0;
        int numberOfTries = 0;
        while (guess != theNumber) {
            System.out.println("Guess a number between 1 and 100:");
            guess = scan.nextInt();
            numberOfTries = numberOfTries + 1;
            if (guess < theNumber) {
                System.out.println(guess + " is too low. Try again.");
            } else if (guess > theNumber) {
                System.out.println(guess + " is too high. Try again.");
            } else {
                System.out.println(guess + " is correct. You win! You took " + numberOfTries + " tries.");
            }
        }//end of while loop for guessing 
        System.out.println("Would you like to play again (y/n)?");
        playAgain = scan.next();
    } while (playAgain.equalsIgnoreCase("y"));
    System.out.println("Thanks for playing! Goodbye!");
    scan.close();
}

Welcome to the world of Java!欢迎来到 Java 的世界!

The code itself has no errors, but it doesn't work the way we want it to.代码本身没有错误,但它没有按照我们希望的方式工作。 Let's find out what the problem is.让我们找出问题所在。

    while (changeDifficulty == true) {

        System.out.println("Welcome to the number guessing game! \nSelect your difficulty (easy, medium, or hard.)");
        String difficulty = "";
        difficulty = scan.nextLine();

        if (difficulty.equalsIgnoreCase("easy")) {
            theNumber = (int)(Math.random() * 100 + 1);
        } else if (difficulty.equalsIgnoreCase("medium")) {
            theNumber = (int)(Math.random() * 1000 + 1);
        } else if (difficulty.equalsIgnoreCase("hard")) {
            theNumber = (int)(Math.random() * 10000 + 1); 
        }

        String correctDifficulty = "";

        if (difficulty.equalsIgnoreCase("easy")) {
            System.out.println("You have selected " + difficulty + " meaning you must guess a number from 1-100"
                    + " is this okay? (y/n)");
            correctDifficulty = scan.nextLine();
            if (correctDifficulty.equalsIgnoreCase("n")) {
                changeDifficulty = false;
            }
        } else if (difficulty.equalsIgnoreCase("medium")) {
            System.out.println("You have selected " + difficulty + " meaning you must guess a number from 1-1000"
                    + " is this okay? (y/n)");
            correctDifficulty = scan.nextLine();
            if (correctDifficulty.equalsIgnoreCase("n")) {
                changeDifficulty = false;
            }
        } else if (difficulty.equalsIgnoreCase("hard")) {
            System.out.println("You have selected " + difficulty + " meaning you must guess a number from 1-10000"
                    + " is this okay? (y/n)");
            correctDifficulty = scan.nextLine(); 
            if (correctDifficulty.equalsIgnoreCase("n")) {
                changeDifficulty = false;
            }
        }

    }

I've found a suspicious part of this, so let's go through it one by one.我发现了一个可疑的部分,所以让我们一一进行。 if the player presses ' n ' [Replace Difficulty] when the code you've raised goes into the 'Replace Difficulty' step, the changeDiffinity will be fixed to false, and therefore the while(changeDifficulty ==true) loop will be ignored for each game.如果玩家在你提出的代码进入“替换难度”步骤时按下“ n ”[替换难度],则changeDiffinity将被固定为false,因此while(changeDifficulty ==true)循环将被忽略每场比赛。

Therefore, our player continues to play with the same numbers and the same level of difficulty.因此,我们的玩家继续以相同的数字和相同的难度进行游戏。

Let's modify our code to Player can change the difficulty level normally and reset the number and the sentence output (1 and 100) so that and after the difficulty level change!让我们修改我们的代码让玩家可以正常更改难度级别并重置数字和句子输出(1和100),以便在难度级别更改之后!

it's one way to put in a new Boolean called changeDifficient, but how about comparing the correctDifficiency that the player is typing in?这是一种放入名为 changeDifficient 的新布尔值的方法,但是如何比较玩家输入的正确性? As follows.如下。

while(changeDifficulty == true) -> do{  }while(correctDifficulty.equalsIgnoreCase("n"));

Like this像这样

 String correctDifficulty = "";
 String numberRange ="";
...
...



 do  {

     System.out.println("Welcome to the number guessing game! \n"
     + "Select your difficulty (easy, medium, or hard.)");

     difficulty = scan.nextLine();


     if (difficulty.equalsIgnoreCase("easy")) {
             theNumber = (int)(Math.random() * 100 + 1);
             numberRange = "1 to 100";
      } else if (difficulty.equalsIgnoreCase("medium")) {
             theNumber = (int)(Math.random() * 1000 + 1);
             numberRange = "1 to 1000";
      } else if (difficulty.equalsIgnoreCase("hard")) {
             theNumber = (int)(Math.random() * 10000 + 1); 
             numberRange = "1 to 10000";
       }

       //Change Difficulty 
        System.out.println("You have selected " + difficulty +
                            " and it means you must guess a number from "+ numberRange  //It depends on the level of difficulty.
                   + " is this okay? (y/n)");

       correctDifficulty = scan.nextLine();


       }while(correctDifficulty.equalsIgnoreCase("n"));

在此处输入图片说明

I hope it helped you a little bit and if it's any a little hard to understand, please leave a comment!希望对你有所帮助,如果有什么难理解的,请留言! Hope you have a peaceful day!希望你有一个平静的一天!



It's an adaptation of the entire code.它是对整个代码的改编。 However, I strongly recommend that you try your own way before you see this code.但是,我强烈建议您在看到此代码之前尝试自己的方式。

Because it's not the perfect way either.因为这也不是完美的方式。

import java.util.Scanner;

public class StackOver
{

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

          //Create a random number for the user to guess
        String playAgain = "";
        int theNumber = 0;
        String difficulty = "";
        String correctDifficulty = "";
        String numberRange ="";

            do {


                do  {

                    System.out.println("Welcome to the number guessing game! \n"
                            + "Select your difficulty (easy, medium, or hard.)");

                    difficulty = scan.nextLine();


                    if (difficulty.equalsIgnoreCase("easy")) {
                        theNumber = (int)(Math.random() * 100 + 1);
                        numberRange = "1 to 100";
                    } else if (difficulty.equalsIgnoreCase("medium")) {
                        theNumber = (int)(Math.random() * 1000 + 1);
                        numberRange = "1 to 1000";
                    } else if (difficulty.equalsIgnoreCase("hard")) {
                        theNumber = (int)(Math.random() * 10000 + 1); 
                        numberRange = "1 to 10000";
                    }

                    //Change Difficulty 
                    System.out.println("You have selected " + difficulty +
                            " and it means you must guess a number from "+ numberRange  //It depends on the level of difficulty.
                            + " is this okay? (y/n)");

                    correctDifficulty = scan.nextLine();


                }while(correctDifficulty.equalsIgnoreCase("n"));


            System.out.println(theNumber); //oㅁo!


            int guess = 0;
            int numberOfTries = 0;
            while (guess != theNumber) {
                System.out.println("Guess a number between " + numberRange);
                guess = scan.nextInt();

                numberOfTries = numberOfTries + 1;
                //numberOfTries += 1;
                //numberOfTries ++; 
                //also make sense and shorter!

                if (guess < theNumber) {
                    System.out.println(guess + " is too low. Try again.");
                } else if (guess > theNumber) {
                    System.out.println(guess + " is too high. Try again.");
                } else {
                    System.out.println(guess + " is correct. You win! You took " + numberOfTries + " tries.");
                }
            }//end of while loop for guessing 

            System.out.println("Would you like to play again (y/n)?");
            playAgain = scan.next();

        } while (playAgain.equalsIgnoreCase("y"));
        System.out.println("Thanks for playing! Goodbye!");
        scan.close();
    }

 }

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

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