import java.util.Scanner;

/**
 * Various examples of loops.
 *
 * The follow code snippets are solutions to the following problems:
 *
 * 1) Print out all the words to the song "99 Bottles of Beer"
 * 2) Print all the powers of 2 that are less than 5000.
 * 3) Echo each line the user types until they enter nothing.
 * 4) Print backwards all the positive even integers starting with 100.
 *    (That is: 100, 98, 96, 94.... 2.)
 * 5) Print the numbers 1 through 10 on one line, with a comma after each
 *    except the last one.  So, the list should be look exactly like:
 *    1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
 *
 * 6) Hi-Lo number game: The computer picks an int between 1 and 100
 *    and then tells the user whether each guess is too high or low until
 *    the user guesses the number correctly.
 * 7) [Challenge] The reverse of 4: the user picks the number and tells
 *    the computer whether its guesses are too high or too low until
 *    the computer guesses it right.  Done correctly, the program should
 *    be able to determine the number within 7 guesses (assuming the human
 *    doesn't cheat).
 *
 * @author Zach Tomaszewski
 * @version 24 Feb 2009
 */
public class LoopExamples {

  public static void main(String[] args) {

    //scanner for use below
    Scanner keybd = new Scanner(System.in);

    //Uncomment each simple example below to run it.

/*
    //
    // 1) Print out all the words to the song "99 Bottles of Beer"
    //
    for (int x = 99; x > 0; x--) {
      System.out.println(x + " bottles of beer on the wall");
      System.out.println(x + " bottles of beer");
      System.out.println("Take one down, pass it around");
      System.out.println((x - 1) + " bottles of beer on the wall.");
      System.out.println();
    }
*/


/*
    //
    // 2) Print all the powers of 2 that are less than 5000.
    // So, we want: 2, 4, 8, 16, 32, 64, ... 4096.
    //
    int power = 2;
    int exponent = 1;  //optional; we really only need power
    while (power < 5000) {
      System.out.println("2^" + exponent + " = " + power);
      power *= 2;
      exponent++;
    }
*/
/*
    // 2b) This could be done as a simple for loop, actually:
    for (int power = 2; power < 5000; power *= 2) {
      System.out.println(power);
    }
*/
/*
    // 2c) In fact, we could even do the first version as a for loop
    // However, this is quite complex and so the first form, or some mix,
    // would probably be clearer.
    for (int power = 2, exponent = 1; power < 5000; power *= 2, exponent++) {
      System.out.println("2^" + exponent + " = " + power);
    }
*/


/*
    //
    // 3) An echo game: take whatever the user types and print it back to them.
    // Keep doing this until they enter nothing.
    //
    String input = "something";   //so that we enter the loop the first time
    while (!input.equals(""))  {  //or use length or isEmpty methods
      System.out.print("Enter something: ");
      input = keybd.nextLine();
      System.out.println(input);
    }
    //this version does print the empty string out
*/
/*
    // 3b) Sometimes it's simpler just to use a boolean variable to control
    // your while loop
    boolean keepAsking = true;
    while (keepAsking)  {
      System.out.print("Enter something: ");
      String input = keybd.nextLine();
      if (input.length() == 0) {
        keepAsking = false;
      }else {
        System.out.println(input);
      }
    }
    //this version stops printing as soon as the user enters nothing
*/


/*
    //
    // 4) Print backwards all the positive even integers starting with 100.
    // (That is: 100, 98, 96, 94.... 2.)
    //
    for (int i = 100; i > 0; i--) {
      //go backwards through all positive ints from 100 through 1
      //printing only the even ones
      if (i % 2 == 0) {
        System.out.println(i);
      }
    }
*/
/*
    //4b) Or, we could just hit only the even numbers using the for loop
    for (int i = 100; i >= 2; i -= 2) {
      System.out.println(i);
    }
*/


/*
    //
    // 5) Print the numbers 1 through 10 on one line, with a comma after
    // each except the last one.  So, the list should be look exactly like:
    // 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
    //
    int i; //declare i out here so we can use it after the loop
    for (i = 1; i < 10; i++) {
      System.out.print(i + ", ");
    }
    System.out.println(i + ".");
*/

/*
    // 5b) Or, if we wanted to keep i within the loop
    for (int i = 1; i <= 10; i++) {
      System.out.print(i);
      if (i < 10) {
        System.out.print(", ");
      }
    }
    System.out.println(".");
*/


/*
    //
    // 6) Hi-lo guessing game.  The computer picks an int between 1 and 100
    // and then tells the user whether each guess is high or low until
    // they guess the number correctly.
    //
    int target = (int) (Math.random() * 100) + 1;  //1 to 100
    int guess = 0;
    System.out.println("Try to guess what number I have in mind (1 to 100).");
    while (guess != target) {
      System.out.print("Enter your guess: ");
      //XXX: not bothering with exception handling
      guess = keybd.nextInt();
      if (guess < target) {
        System.out.println("Too low.");
      }else if (guess > target) {
        System.out.println("Too high.");
      }
    }
    //can only get here when loop ends: guess == target
    System.out.println("You guessed it!");
*/


/*
    //
    // 7) The user picks a number between 1 and 100 and tells the computer
    // whether its guesses are too high or too low until the computer guesses
    // it right.
    //
    int lowerBound = 0;
    int upperBound = 101;
    boolean guessedIt = false;
    System.out.println("Pick a number between 1 and 100 " +
                       "and I'll try to guess it.");

    //Going to use a binary search to zero in on the number
    while (!guessedIt) {
      //determine and display guess
      int guess = (upperBound + lowerBound) / 2;
      System.out.println("Is the number... " + guess + "?");

      //get feedback from user
      System.out.println("1 = Too Low");
      System.out.println("2 = Too High");
      System.out.println("0 = Correct!");
      System.out.print("How did the computer do? ");
      //XXX: Again not bothering with exception handling
      int feedback = keybd.nextInt();
      System.out.println();

      //determine what feedback means
      if (feedback == 0) {
        //yay, got it!
        guessedIt = true;
        System.out.println("So your number was " + guess + ".");
      }else if (feedback == 1) {
        //guess was too low, so never go lower than this
        lowerBound = guess;
      }else if (feedback == 2) {
        //guess was too high, so never go higher than that
        upperBound = guess;
      }else {
        //bad numerical user input
        System.out.println("Huh?");
        //repeat guess
      }
    }
*/

  }
}

