Advertisement
mmayoub

Loops - execise no 07 slide no 69

Jun 28th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. package class170629;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class LoopsEx07Slide69 {
  6.  
  7.     public static void main(String[] args) {
  8.         // carpet
  9.         // input: integer number - the size of the carpet
  10.         // output: draw a carpet in the selected size
  11.  
  12.         // create a scanner
  13.         Scanner s = new Scanner(System.in);
  14.         // ask for and get the carpet size
  15.         System.out.print("Enter the carpet size: ");
  16.         int n = s.nextInt(); // input: the carpet size from the user
  17.  
  18.         // in each row or column there is : n*n astricks + (n-1) spaces
  19.         int size = n * n + n - 1; // calculated:number of rows and columns
  20.  
  21.         // for each row
  22.         for (int row = 0; row < size; row += 1) {
  23.             // if it is a blank row
  24.             if (row % (n + 1) == n) {
  25.                 // just go to a new row
  26.                 System.out.println();
  27.             } else {
  28.                 // its not a blank row, so for each column
  29.                 for (int col = 0; col < size; col += 1) {
  30.                     // print one char:
  31.                     // space after each block
  32.                     // or astrick
  33.                     System.out.print(col % (n + 1) == n ? " " : "*");
  34.                 }
  35.                 // start a new row
  36.                 System.out.println();
  37.             }
  38.  
  39.         }
  40.         // close the scanner
  41.         s.close();
  42.  
  43.     }
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement