Guest User

Untitled

a guest
Feb 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2.  
  3. public class Weekly_Program_04 {
  4.  
  5.     public static void main(String[] args) {
  6.        
  7.         Scanner stdIn = new Scanner(System.in);
  8.        
  9.         System.out.println("Please wait while the computer picks a number between 0 and 255...");
  10.        
  11.         int secret = (int)(Math.random() * 256);
  12.         int guess = 0;
  13.        
  14.         System.out.print("Please enter a guess [0, 255] : " + '\r');
  15.         guess = stdIn.nextInt();
  16.        
  17.         int x = 1; //counts the number of guesses
  18.        
  19.         while (guess <=0 || guess >=255)
  20.         {
  21.             System.out.print(guess + " is not a valid guess.");
  22.             System.out.print('\r'+ "Please enter a guess [0, 255] : " + '\r');
  23.             guess = stdIn.nextInt();           
  24.         }
  25.        
  26.         while (guess != secret)
  27.         {
  28.             while (guess < secret)
  29.             {
  30.                 System.out.println("Too low!");
  31.                 System.out.print('\r'+ "Please enter a guess [0, 255] : ");
  32.                 guess = stdIn.nextInt();
  33.                 x++;
  34.             }
  35.             while (guess > secret)
  36.             {
  37.                 System.out.println("Too high!");
  38.                 System.out.print('\r'+ "Please enter a guess [0, 255] : ");
  39.                 guess = stdIn.nextInt();
  40.                 x++;                   
  41.             }
  42.         }
  43.        
  44.         System.out.println("You got it!");
  45.         System.out.print('\r'+ "You guessed the secret number " + secret + " in " + x + " guesses.");
  46.         System.out.print('\r'+ "The worst you should have done was 8 guesses.");
  47.        
  48.         if (x > 8)
  49.             System.out.print('\r' + "Better luck next time!");
  50.         else
  51.             System.out.print('\r' + "Nice job!");
  52.        
  53.     }
  54.  
  55. }
Add Comment
Please, Sign In to add comment