Advertisement
Guest User

Assignment2_4

a guest
Oct 21st, 2012
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 KB | None | 0 0
  1. // Joshua Ellis
  2. // 10/21/2012
  3. //This program prints a grid of stars and zeros based on the size entered from 5-21.
  4. // E.G.
  5. // 0******
  6. // *0*****
  7. // **0****
  8. // ***0***
  9. // ****0**
  10. // *****0*
  11. // ******0
  12.  
  13. import java.util.Scanner;
  14.  
  15. public class Assignment2_4 {
  16.  
  17.     public static void main(String[] args) {
  18.  
  19.         // objects and variables
  20.         Scanner input = new Scanner(System.in);
  21.         int drawingSize = 5;
  22.         char keepGoing = 'Y';
  23.         String[] drawEachRow;
  24.  
  25.         System.out.println("Drawing Program");
  26.         System.out.println("===============");
  27.  
  28.         // keep looping until user inputs something other than Y or y
  29.         while (keepGoing == 'Y' || keepGoing == 'y') {
  30.             System.out.print("How many rows/columns (5-21)? ");
  31.             drawingSize = input.nextInt();
  32.  
  33.             // check input for proper range
  34.             while (drawingSize < 5 || drawingSize > 21) {
  35.                 System.out.print("Out of Range! Re-enter: ");
  36.                 drawingSize = input.nextInt();
  37.             }
  38.  
  39.             // initialize array based on size of drawing
  40.             drawEachRow = new String[drawingSize];
  41.             for (int i = 0; i < drawingSize; i++) {
  42.                 drawEachRow[i] = "*";
  43.             }
  44.  
  45.             System.out.println();
  46.  
  47.             // display drawing
  48.             for (int rows = 0; rows < drawingSize; rows++) {
  49.                 for (int columns = 0; columns < drawingSize; columns++) {
  50.  
  51.                     // place zeros in each correct spot based on the current
  52.                     // row. This adds zeros in every position UP TO and
  53.                     // including the current row position (e.g. row 2 > position
  54.                     // 2)
  55.                     drawEachRow[rows] = "0";
  56.  
  57.                     // print each row
  58.                     System.out.print(drawEachRow[columns]);
  59.  
  60.                     // this removes all 0s from the array so it contains only
  61.                     // *s. This is so that the previously added 0s for each row
  62.                     // are removed so the new row is correct.
  63.                     for (int arraySize = 0; arraySize < drawingSize; arraySize++) {
  64.                         drawEachRow[arraySize] = "*";
  65.                     }
  66.                 }
  67.                 // new line after each row
  68.                 System.out.println();
  69.             }
  70.  
  71.             // keep going?
  72.             System.out.print("Do you want to continue (Y/N)? ");
  73.             keepGoing = input.next().charAt(0);
  74.         }
  75.         System.out.println("Good bye!");
  76.         input.close();
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement