import java.util.Scanner;

/**
 * More examples of loops, mostly dealing with nested loops.
 *
 * Problems:
 * <pre>
 * 1) Print all the positive integers less than 100
 * that are divisible by 3.
 *
 * Given a user-specified width:
 * 2) Print a line of "width" asterisks (*)
 *    That is, if the user enters 3, print 3 asterisks.
 * 3) Print a square box of *s with the given width (and height)
 *    That is, if the user enters 3, prints:
 *      ***
 *      ***
 *      ***
 * 4) Print a box with a hole in its center.
 *    That is, print a box as in #3 but with the single * in the center
 *    (or one close to the center for boxes with an even width) replaced
 *    with a space.
 * 4+) 4 looks bad for even boxes.  Fix this so that there is a hole of 4
 *    spaces in the center of a large even-width box. [HARD]
 *
 * 5) Print an equilateral triangle of *s with the given width.
 *    The right angle should be on the lower left.  So, given a width of 4:
 *      *
 *      **
 *      ***
 *      ****
 * 6) Print an equilateral triangle of *s with the given width.
 *    The right angle should be on the lower right.  So, given a width of 4:
 *         *
 *        **
 *       ***
 *      ****
 * 7) Print an equilateral triangle of *s with the given width.
 *    The right angle should be on the upper left.  So, given a width of 4:
 *      ****
 *      ***
 *      **
 *      *
 * 8) Print an equilateral triangle of *s with the given width.
 *    The right angle should be on the upper right.  So, given a width of 4:
 *      ****
 *       ***
 *        **
 *         *
 *
 * </pre>
 *
 * Often, more than one example solution is given just to illustrate different
 * ways you might think about the problem.  If you want to test or play around
 * with a solution, just uncomment that one solution, compile, and run.
 *
 * @author Zach Tomaszewski
 * @version 26 Feb 2009
 */

public class MoreLoopExamples {

  public static void main(String[] args) {


    //
    // 1) Print all the positive integers less than 100
    // that are divisible by 3.
    //
/*
    //1a: go through the numbers 1 to 100,
    //printing each only if it's divisible by 3

    for (int i = 1; i < 100; i++) {
      if (i % 3 == 0) {
        System.out.println(i);
      }
    }
*/
/*
    //1b: Go through only the numbers we know we'll have to print
    //That is, start at 3 and print every 3rd number from there

    for (int i = 3; i < 100; i += 3) {
      System.out.println(i);
    }
*/
/*
    //1c: Who said we need to start at the bottom?
    //This is as 1a, but prints in reverse.

    for (int i = 100; i > 0; i--) {
      if (i % 3 == 0) {
        System.out.println(i);
      }
    }
*/
/*
    //1d: An interesting student-suggested approach,
    //this for loop effectively goes from 1 to 33

    for (int i = 1; (i * 3) < 100; i++) {
      System.out.println(i * 3);
    }
*/


    //Ask user for a width to be used for next 7 problems.
    Scanner keybd = new Scanner(System.in);
    System.out.print("Enter a width: ");
    int width = keybd.nextInt();

    //
    // 2) Print a line of "width" asterisks (*)
    //
/*
    //2a: Pretty standard for loop.

    for (int i = 0; i < width; i++) {
      System.out.print("*");
    }
    //terminate the line properly when we're done
    System.out.println();
*/
/*
    //2b: We don't always need to start at 0, or use only < or >
    //(If your loops ever produces one more or one less iteration than you
    //intended, look at the starting value and whether you used <= or <)

    for (int i = 1; i <= width; i++) {
      System.out.print("*");
    }
    System.out.println();
*/
/*
    //2c: Assuming we don't need the value in width again after the loop
    //we could use it directly as the loop counter.

    while (width > 0) {
      System.out.print("*");
      width--;
    }
    System.out.println();
*/
/*
    //2d: This loop does essentially the same thing as 2c.
    //While more concise than 2c, it is still probably a bad idea:
    //it fails to follow coding stardards and is (arguably) harder
    //to understand.

    while (width-- > 0) System.out.print("*");
    System.out.println();
*/
/*
    //2e: Note that all the loops above don't print anything if given
    //a negative number.  This one will print about 4 billion *s before
    //stopping if given a width of -1.  Otherwise identical to 2a,
    //this demonstrates the subtle difference between using != and <
    //It still works fine for positive widths though.

    for (int i = 0; i != width; i++) {
      System.out.print("*");
    }
    System.out.println();
*/


    //
    // 3) Print a square box of *s with the given width
    //
/*
    //3a: Basically, we went to repeat printing a line (as in 2a)
    //"width" times.  So we should just put another loop around the 2a code.
    //Note that the loop counters here actually correspond to the (x,y)
    //position of the * currently being printed.  The upper-left * in a box
    //would be at (0,0), and the lower-right would be at
    //(width - 1, width - 1).
    for (int y = 0; y < width; y++) {
      for (int x = 0; x < width; x++) {
        System.out.print("*");
      }
      //try moving this println into the for loop above or after
      //the brace below and see what difference it makes.
      System.out.println();
    }
*/
/*
    //3b: Here, we just have one loop to print out all the *s we'll need
    //in the box.  But I still need the line breaks, so I insert that after
    //any * that is "divisible" by the width.
    for (int i = 1; i <= width * width; i++) {
      System.out.print("*");
      if (i % width == 0) {
        System.out.println();
      }
    }
*/


    //
    // 4) Print a box with a hole in its center.
    //
/*
    //4a: The coordinates of the center of a box is (width/2, width/2).
    //Remember, this is int math and we're counting from 0, so 5/2 will
    //give us 2, which is the third * along in the line.
    //So we need to print a space only when x == width/2 and y == width/2.
    //In all other cases, we should just print a * for that spot.

    for (int y = 0; y < width; y++) {
      //print a line
      for (int x = 0; x < width; x++) {
        //What happens if we have only the x condition or
        //only the y condition as our test below?
        //What happens if we use an || rather than an &&?
        if (x == width / 2 && y == width / 2) {
          System.out.print(" ");
        }else {
          System.out.print("*");
        }
      }
      System.out.println();
    }
*/

    //
    //4+) 4 looks bad for even boxes.  Fix this so that there is a hole of 4
    //   spaces in the center of a large even-width box.
    //
/*
    //We just need to modify our conditions used in 4.
    //We need four spaces for even widths, but we still only want 1 space
    //for odd widths.
    //There's no way to put a good looking hole in boxes of width 2
    //or smaller though, so we won't try putting a hole in a box that small.
    for (int y = 0; y < width; y++) {
      for (int x = 0; x < width; x++) {
        if (width > 2 && width % 2 == 1 &&
            (x == width / 2 && y == width / 2)) {
          //the center of any large odd-width square
          System.out.print(" ");
        }else if (width > 2 && width % 2 == 0) {
          //currently printing an even-width square, so
          //determine what we should be printing.
          int mid = width / 2;
          if ((x == mid || x == mid - 1) && (y == mid || y == mid - 1)) {
            //one of the four center squares
            System.out.print(" ");
          }else {
            //some other square
            System.out.print("*");
          }
        }else {
          System.out.print("*");
        }
      }
      System.out.println();
    }
*/


    //
    // 5) Print an equilateral triangle of *s with the given width.
    // The right angle should be on the lower left.
    //
/*
    //5a: This is another case of printing a box with some characters
    //replaced with spaces.

    for (int y = 0; y < width; y++) {
      for (int x = 0; x < width; x++) {
        if (x > y) {
          System.out.print(" ");
        }else {
          System.out.print("*");
        }
      }
      System.out.println();
    }
*/
/*
    //5b: Technically, we don't need to print the spaces,
    //since they can't be seen anyway.

    for (int y = 0; y < width; y++) {
      for (int x = 0; x < width; x++) {
        System.out.print("*");
        if (x == y) {
          System.out.println();
          break; //break out of the inner for loop
        }
      }
    }
*/

    //
    // 6) Print an equilateral triangle of *s with the given width.
    // The right angle should be on the lower right.
    //
/*
    //6: We'll have to print the space this time.
    //We just need to change the inner conditional from 5a.

    for (int y = 0; y < width; y++) {
      for (int x = 0; x < width; x++) {
        if (x < width - y - 1) {
          System.out.print(" ");
        }else {
          System.out.print("*");
        }
      }
      System.out.println();
    }
*/

    //
    //7, 8) These I'm leaving as an exercise for you to do.
    //Like the change from 5 to 6, you'll need to figure out what the
    //inner condition needs to be.  Try running your program with
    //widths from 0 through 6 to see if it's working.
    //

  }
}









