scribbledecobble
scribbledecobble.blogspot.com
Pages
Home
Contents
A
…
K
Guessing Game
import java.util.Scanner; public class GuessingGame { public static void main(String args[]) { final int MAX = 100; final int GUESS_LIMIT = 20; final Scanner scanner = new Scanner(System.in); final Double d = Math.random() * MAX; final int n = d.intValue(); System.out.print("I'm thinking of a number between 0 and " + MAX); System.out.println(". Guess what number I'm thinking of."); int count = 1; int guess = scanner.nextInt(); while (guess != n && count < GUESS_LIMIT) { if (guess < n) { System.out.println("Too low."); } else { System.out.println("Too high."); } guess = scanner.nextInt(); count++; } if (guess == n) { System.out.print("That's right! It took you "); System.out.print(count); System.out.println(" guesses."); } else { System.out.println("Too many guesses."); } scanner.close(); } }
Newer Post
Older Post
Home