// Joshua Ellis // 10/21/2012 //This program prints a grid of stars and zeros based on the size entered from 5-21. // E.G. // 0****** // *0***** // **0**** // ***0*** // ****0** // *****0* // ******0 import java.util.Scanner; public class Assignment2_4 { public static void main(String[] args) { // objects and variables Scanner input = new Scanner(System.in); int drawingSize = 5; char keepGoing = 'Y'; String[] drawEachRow; System.out.println("Drawing Program"); System.out.println("==============="); // keep looping until user inputs something other than Y or y while (keepGoing == 'Y' || keepGoing == 'y') { System.out.print("How many rows/columns (5-21)? "); drawingSize = input.nextInt(); // check input for proper range while (drawingSize < 5 || drawingSize > 21) { System.out.print("Out of Range! Re-enter: "); drawingSize = input.nextInt(); } // initialize array based on size of drawing drawEachRow = new String[drawingSize]; for (int i = 0; i < drawingSize; i++) { drawEachRow[i] = "*"; } System.out.println(); // display drawing for (int rows = 0; rows < drawingSize; rows++) { for (int columns = 0; columns < drawingSize; columns++) { // place zeros in each correct spot based on the current // row. This adds zeros in every position UP TO and // including the current row position (e.g. row 2 > position // 2) drawEachRow[rows] = "0"; // print each row System.out.print(drawEachRow[columns]); // this removes all 0s from the array so it contains only // *s. This is so that the previously added 0s for each row // are removed so the new row is correct. for (int arraySize = 0; arraySize < drawingSize; arraySize++) { drawEachRow[arraySize] = "*"; } } // new line after each row System.out.println(); } // keep going? System.out.print("Do you want to continue (Y/N)? "); keepGoing = input.next().charAt(0); } System.out.println("Good bye!"); input.close(); } }