Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 9th, 2012  |  syntax: None  |  size: 2.52 KB  |  views: 88  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.util.Scanner;
  2. import java.util.Random;
  3.  
  4. public class GuessingGame{
  5.     public static void main(String[] args){
  6.         Scanner scan = new Scanner(System.in);
  7.         String input;
  8.         boolean playing = true;
  9.         System.out.println("Pick a number from 1 to 100, and I will try to guess it!");
  10.         while(playing){
  11.             Random rand = new Random();
  12.             int high = 100;
  13.             int low = 0;
  14.             int guessesTaken = 0;
  15.             int guess;
  16.             boolean correct = false;
  17.             boolean answered = false;
  18.             while(!correct){
  19.                 if(high - low - 1 > 0){
  20.                     guess = rand.nextInt(high - low - 1) + low + 1;
  21.                 }
  22.                 else{
  23.                     answered = true;
  24.                     correct = true;
  25.                     guess = 0;
  26.                     System.out.println("You're a liar!");
  27.                 }
  28.                 guessesTaken++;
  29.                 while(!answered){
  30.                     System.out.printf("\nIs %s your number? [Yes (y) / No, mine is higher (h) / No, mine is lower (l)]\n", guess);
  31.                     input = scan.next();
  32.                     if(input.equalsIgnoreCase("y")){
  33.                         correct = true;
  34.                         System.out.printf("Yay, I guessed correctly! And it only took %s guesses!\n", guessesTaken);
  35.                         answered = true;
  36.                     }
  37.                     else if(input.equalsIgnoreCase("h")){
  38.                         System.out.println("Dang.");
  39.                         low = guess;
  40.                         answered = true;
  41.                     }
  42.                     else if(input.equalsIgnoreCase("l")){
  43.                         System.out.println("Dang.");
  44.                         high = guess;
  45.                         answered = true;
  46.                     }
  47.                 }
  48.                 answered = false;
  49.             }
  50.            
  51.             answered = false;
  52.             while(!answered){
  53.                 System.out.println("Do you want to play again? [Yes (y) / No (n) ]");
  54.                 input = scan.next();
  55.                 if(input.equalsIgnoreCase("n")){
  56.                     playing = false;
  57.                     answered = true;
  58.                 }
  59.                 else if(input.equalsIgnoreCase("y")){
  60.                     playing = true;
  61.                     answered = true;
  62.                     System.out.println("Great! Pick another number from 1 to 100.");
  63.                 }
  64.             }
  65.         }
  66.     }
  67. }