简体   繁体   中英

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. 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!

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.

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!

it's one way to put in a new Boolean called changeDifficient, but how about comparing the correctDifficiency that the player is typing in? 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();
    }

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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