简体   繁体   中英

number guessing game java

I'm writing a java code that asks the user to guess the computer's random number, which I have fine, however, what I would like to do is have a message that corresponds to the number of guesses it took the user.

For example if the user guessed the random number in 1 try there would be in output of "Excellent!" if the user guessed the answer in 2-4 tries "Good Job" and so on..

I guess I haven't tried anything, because I'm not sure where to stick the code?
I know it would have to be if guess == 1 then do this else if code >1<=3 then do this and so on.. but where in my code? should it be in the while loop with the nested if's as well?

Here is my updated code, it runs and compiles the exact way I needed to create it. Thanks for all of the help!

public static void main(String[] args) {
        Random rand = new Random();
        int compNum = rand.nextInt(100);
        int count = 0;
        Scanner keyboard = new Scanner(System.in);
        int userGuess;  
        boolean win = false;        
        while (win == false ){
            System.out.print("Enter a guess between 1 and 100: ");
            userGuess = keyboard.nextInt();
            count++;
            if(userGuess < 1 || userGuess > 100){
                System.out.println("Your guess is out of range.  Pick a number between 1 and 100.");
                System.out.println();
            }
            else if (userGuess == compNum){
                win = true;
                System.out.println("Congratulations!  Your answer was correct! ");
            }       
            else if (userGuess < compNum){
                System.out.println("Your guess was too low.  Try again. ");
                System.out.println();
            }
            else if (userGuess > compNum){
                System.out.println("Your guess was too high.  Try again. ");
                System.out.println();
            }

        }
        if(count == 1){
            System.out.println();
            System.out.println("I had chosen " + compNum + " as the target number.");
            System.out.println("You guessed it in " + count + " tries.");
            System.out.println("That was lucky!");
        }
        else if (count > 1 && count <= 4){
            System.out.println();
            System.out.println("I had chosen " + compNum + " as the target number.");
            System.out.println("You guessed it in " + count + " tries.");
            System.out.println("That was amazing!");
        }
        else if (count > 4 && count <= 6){
            System.out.println();
            System.out.println("I had chosen " + compNum + " as the target number.");
            System.out.println("You guessed it in " + count + " tries.");
            System.out.println("That was good.");
        }
        else if (count > 6 && count <= 7){
            System.out.println();
            System.out.println("I had chosen " + compNum + " as the target number.");
            System.out.println("You guessed it in " + count + " tries.");
            System.out.println("That was OK.");
        }               
        else if (count > 7 && count <= 9){
            System.out.println();
            System.out.println("I had chosen " + compNum + " as the target number.");
            System.out.println("You guessed it in " + count + " tries.");
            System.out.println("That was not very good.");
        }
        else if (count > 10){
            System.out.println();
            System.out.println("I had chosen " + compNum + " as the target number.");
            System.out.println("You guessed it in " + count + " tries.");
            System.out.println("This just isn't your game.");

        }
    }
        }

You could count the iterations. Something like.

boolean notCorrect = true;
int guesses = 0;
while(notCorrect){
    //Code for checking user input. 
    //break out if true
    guesses++;
}

Sorry did that backwards.

elsewhere

if(guesses < 2) {
    // display message
}
// include other if's to determine which message to display.

You could put the if statement that decides which message to display in the if that checks whether the guess is correct. But pull that out of the loop. That way your only running that code if the user actually guessed correctly.

if (userGuess == compNum){
    win = true;
    System.out.println("Congratulations! Your answer was correct! ");
    // put message decision here...
}

Keep a count in an int i; and every time the user guesses wrong (not if the user gets the answer right, do i++ . Then, have something like the following where you respond ti the correct answer:

if(i == 0) System.out.println("Perfect!");    //Got it the first time
else if(i == 1) System.out.println("Nice!");  //Got it on the second try
else if(i <= 4) System.out.println("Good Job!");   //Got it between the second and fifth time
else if(i <= 8) System.out.println("Okay!");   //Got it between 6th and 9th time
else System.out.println("Hmmm... Try to do better next time!");  //took more than 10 times to get it right

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