Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner; //Only using Scanner
- public class numberGuess
- {
- static Scanner console = new Scanner(System.in);
- public static void main(String[] args)
- {
- int lowerBound=0;
- int upperBound=999;
- int nextGuess=0;
- int tries=0;
- char choice = 'a';
- System.out.println("Think of a number between 0 and 999.");
- nextGuess = ( (lowerBound + upperBound)/2 );
- 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
- choice = console.next().charAt(0);
- System.out.println("FIRST");
- tries++;
- do
- {
- switch (choice)
- {
- case 'y':
- System.out.println("I guessed your number " + nextGuess + " in " + tries + " tries.");
- break;
- case 'l': //If it is lower then 499 becomes the upperBound
- upperBound = nextGuess;
- tries++;
- nextGuess = ( (lowerBound + upperBound)/2 );
- System.out.println("Is it " + nextGuess + "? \n\t Enter y if it is, l if it is less, or m if it is more.");
- choice = console.next().charAt(0);
- System.out.println("LOWER");
- break;
- case 'm': //If it is more then 499 becomes the lowerBound
- lowerBound = nextGuess;
- tries++;
- nextGuess = ( (lowerBound + upperBound)/2 );
- System.out.println("Is it " + nextGuess + "? \n\t Enter y if it is, l if it is less, or m if it is more.");
- choice = console.next().charAt(0);
- System.out.println("MORE");
- break;
- default:
- System.out.println("Enter y, lowercase L, or m only! Try again:");
- choice = console.next().charAt(0);
- System.out.println("DEFAULT");
- break;
- }
- }
- while (choice == console.next().charAt(0));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement