forked from DSkilton/UdemyTimBurchalka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiagonalStar.java
62 lines (58 loc) · 1.94 KB
/
DiagonalStar.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.TimBuchalka;
//Write a method named printSquareStar with one parameter of type int named number. If number is < 5, the method
//should print "Invalid Value". The method should print diagonals to generate a rectangular pattern composed of
//stars (*). This should be accomplished by using loops (see examples below).
//
//EXAMPLE 1
//printSquareStar(5); should print the following:
//*****
//** **
//* * *
//** **
//*****
//
//Explanation:
//***** 5 stars
//** ** 2 stars space 2 stars
//* * * 1 star space 1 star space 1 star
//** ** 2 stars space 2 stars
//***** 5 stars
//
//EXAMPLE 2
//printSquareStar(8); should print the following:
//********
//** **
//* * * *
//* ** *
//* ** *
//* * * *
//** **
//********
//
//The patterns above consist of a number of rows and columns (where number is the number of rows to print). For
//each row or column, stars are printed based on four conditions (Read them carefully):
//* In the first or last row
//* In the first or last column
//* When the row number equals the column number
//* When the column number equals rowCount - currentRow + 1 (where currentRow is current row number)
public class DiagonalStar {
public static void printSquareStar(int number){
if( number < 5) {
System.out.println("Invalid number");
return;
}
for (int row = 1; row <= number; row++){
for (int col = 1; col <= number; col++){
if (row == 1 || row == number || col == 1 || col == number){
System.out.print("*");
} else if (row == col) {
System.out.print("*");
} else if (col == (number - row + 1)) {
System.out.print("*");
} else
System.out.print(" ");
}// close inner loop
System.out.println();
}// close outter loop
}
}