mardi 15 juin 2021

How do I do this exactly?

Create a new program named GuessMeFinally that starts with your original guessing game from an earlier practice session. For this version, include the following steps:

Instead of letting the user only try once more if they get it wrong, let them keep going until they get it right.

If they get it on the first try print out "Wow, nice guess! That was it!" otherwise print out "Finally! It's about time you got it!" What You Should See (Example)

I've chosen a number between -100 and 100. Betcha can't guess it! Your guess: 44

Ha, nice try - too low! Try again! Your guess: 99

Too bad, way too high. Try again! Your guess: 74

Ha, nice try - too low! Try again! Your guess: 82

Finally! It's about time you got it!

My code:

public class GuessMeFinally {
    public static void main(String[] args) {
        
        Random randomizer = new Random();
        Scanner scan = new Scanner(System.in);
        
        int num = randomizer.nextInt(100 + 100) - 100;
        int b = scan.nextInt();
        
        System.out.println("I have chosen a number between -100 and 100. Betcha can't guess it!");
        System.out.println("Your guess: ");
        b = scan.nextInt();

        while(b!=num) {
            System.out.println("Your guess: ");
            b = scan.nextInt();
            
            if (b==num) {
            System.out.println(b + "! " + "Wow, nice guess! That was it!");
            break;
            }
            if (b<num) {
            System.out.println(b + "? " + "Ha, nice try - too low! Try again!");
            } else if (b>num) {
                System.out.println(b + "? " + "Too bad, way too high. Try again!");
            }
        }
    }
}



Aucun commentaire:

Enregistrer un commentaire