# Object-Oriented Programming with Java, part I – Exercise 41

This program picks a random number and then lets you guess until you get it.

## My Code

``````
import java.util.Random;
import java.util.Scanner;

public class GuessingNumberGame {

public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int numberDrawn = drawNumber();

int numGuesses = 1;

// program your solution here. Do not touch the above lines!
while (true) {
System.out.println("Guess a number: ");
int guess = Integer.parseInt(reader.nextLine());

if (guess == numberDrawn) {
System.out.println("Congratulations, your guess is correct!");
break;
} else if (guess > numberDrawn) {
System.out.println("The number is lesser, guesses made: " + numGuesses);
} else {
System.out.println("The number is greater, guesses made: " + numGuesses);
}
numGuesses++;
}
}

// DO NOT MODIFY THIS!
private static int drawNumber() {
return new Random().nextInt(101);
}
}
``````

## Model Code

``````import java.util.Random;

import java.util.Scanner;

public class GuessingNumberGame {

public static void main(String[] args) {

Scanner reader = new Scanner(System.in);

int numberDrawn = drawNumber();

// program your solution here. Do not touch the above lines!

int guessesMAde = 0;

while (true) {

System.out.print("Guess a number: ");

int guess = Integer.parseInt(reader.nextLine());

if (guess == numberDrawn) {

break;

} else if (guess < numberDrawn) {

System.out.println("The number is greater, guesses made: " + guessesMAde);

} else {

System.out.println("The number is lesser, guesses made: " + guessesMAde);

}

}

System.out.println("Congratulations, your guess is correct!");

}

// DO NOT MODIFY THIS!

private static int drawNumber() {

return new Random().nextInt(101);

}

}``````