Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class GuessGame {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int secretNum = (int)(Math.random()*100) + 1;
- System.out.printf("Secret number - %d", secretNum);
- System.out.println("\n----------------");
- int numOfGuesses = 0, lowerBound = 1, upperBound = 100, guess = -1;
- System.out.println("This is a guess game. Try to guess a number between 1 to 100." +
- " On success the program will prompt the number of guesses you made.");
- do {
- System.out.printf("Please choose a number between %d and %d\n", lowerBound, upperBound);
- guess = sc.nextInt();
- if(guess < secretNum) {
- System.out.println("Too small...");
- lowerBound = guess;
- } else if(guess > secretNum) {
- System.out.println("Too big...");
- upperBound = guess;
- }
- numOfGuesses += 1;
- } while(guess != secretNum);
- System.out.printf("You guessed right after %d attempts.", numOfGuesses);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement