Advertisement
Icegoten

numberGuess

Jan 29th, 2012
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. import java.util.Scanner;   //Only using Scanner
  2.  
  3. public class numberGuess
  4. {
  5.     static Scanner console = new Scanner(System.in);
  6.  
  7.     public static void main(String[] args)
  8.     {
  9.         int lowerBound=0;
  10.         int upperBound=999;
  11.         int nextGuess=0;
  12.         int tries=0;
  13.         char choice = 'a';
  14.        
  15.         System.out.println("Think of a number between 0 and 999.");
  16.         nextGuess = ( (lowerBound + upperBound)/2 );
  17.         System.out.println("Is it " + nextGuess + "? \n\t Enter y if it is, l if it is less, or m if it is more."); //Starts with 499
  18.         choice = console.next().charAt(0);
  19.         System.out.println("FIRST");
  20.         tries++;
  21.            
  22.                
  23.             do
  24.             {
  25.                 switch (choice)
  26.                 {
  27.                 case 'y':
  28.                     System.out.println("I guessed your number " + nextGuess + " in " + tries + " tries.");
  29.                     break;
  30.                    
  31.                
  32.                 case 'l':   //If it is lower then 499 becomes the upperBound
  33.                     upperBound = nextGuess;
  34.                     tries++;
  35.                     nextGuess = ( (lowerBound + upperBound)/2 );
  36.                     System.out.println("Is it " + nextGuess + "? \n\t Enter y if it is, l if it is less, or m if it is more.");
  37.                     choice = console.next().charAt(0);
  38.                     System.out.println("LOWER");
  39.                     break;
  40.                    
  41.                
  42.                 case 'm':   //If it is more then 499 becomes the lowerBound
  43.                     lowerBound = nextGuess;
  44.                     tries++;
  45.                     nextGuess = ( (lowerBound + upperBound)/2 );
  46.                     System.out.println("Is it " + nextGuess + "? \n\t Enter y if it is, l if it is less, or m if it is more.");
  47.                     choice = console.next().charAt(0);
  48.                     System.out.println("MORE");
  49.                     break;
  50.                    
  51.                    
  52.                 default:
  53.                     System.out.println("Enter y, lowercase L, or m only! Try again:");
  54.                     choice = console.next().charAt(0);
  55.                     System.out.println("DEFAULT");
  56.                     break;
  57.                 }
  58.             }
  59.             while (choice == console.next().charAt(0));
  60.            
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement