Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package class170629;
- import java.util.Scanner;
- public class LoopsEx07Slide69 {
- public static void main(String[] args) {
- // carpet
- // input: integer number - the size of the carpet
- // output: draw a carpet in the selected size
- // create a scanner
- Scanner s = new Scanner(System.in);
- // ask for and get the carpet size
- System.out.print("Enter the carpet size: ");
- int n = s.nextInt(); // input: the carpet size from the user
- // in each row or column there is : n*n astricks + (n-1) spaces
- int size = n * n + n - 1; // calculated:number of rows and columns
- // for each row
- for (int row = 0; row < size; row += 1) {
- // if it is a blank row
- if (row % (n + 1) == n) {
- // just go to a new row
- System.out.println();
- } else {
- // its not a blank row, so for each column
- for (int col = 0; col < size; col += 1) {
- // print one char:
- // space after each block
- // or astrick
- System.out.print(col % (n + 1) == n ? " " : "*");
- }
- // start a new row
- System.out.println();
- }
- }
- // close the scanner
- s.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement