Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Weekly_Program_04 {
- public static void main(String[] args) {
- Scanner stdIn = new Scanner(System.in);
- System.out.println("Please wait while the computer picks a number between 0 and 255...");
- int secret = (int)(Math.random() * 256);
- int guess = 0;
- System.out.print("Please enter a guess [0, 255] : " + '\r');
- guess = stdIn.nextInt();
- int x = 1; //counts the number of guesses
- while (guess <=0 || guess >=255)
- {
- System.out.print(guess + " is not a valid guess.");
- System.out.print('\r'+ "Please enter a guess [0, 255] : " + '\r');
- guess = stdIn.nextInt();
- }
- while (guess != secret)
- {
- while (guess < secret)
- {
- System.out.println("Too low!");
- System.out.print('\r'+ "Please enter a guess [0, 255] : ");
- guess = stdIn.nextInt();
- x++;
- }
- while (guess > secret)
- {
- System.out.println("Too high!");
- System.out.print('\r'+ "Please enter a guess [0, 255] : ");
- guess = stdIn.nextInt();
- x++;
- }
- }
- System.out.println("You got it!");
- System.out.print('\r'+ "You guessed the secret number " + secret + " in " + x + " guesses.");
- System.out.print('\r'+ "The worst you should have done was 8 guesses.");
- if (x > 8)
- System.out.print('\r' + "Better luck next time!");
- else
- System.out.print('\r' + "Nice job!");
- }
- }
Add Comment
Please, Sign In to add comment