I am leaning Java, trying to solve the patterns in Java using for loop Solid Rhombus.
Print Format:
line 1 :5 Spaces *****(5 Stars) then
Line 2 :4 Spaces *****
line 3 :3 Spaces *****
Line 4 :2 Spaces *****
Line 5 :1 Space *****
I tried for loop with nested for loop as below
public class SolidRombus {
public static void main(String[] args) {
int n = 5;
int m = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m-i+1; j++) {
System.out.print(" ");
}
for (int j = 1; j <= m; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Is there any other way to print same Star-pattern using only for-loop with less statements in Java?
Something like this would do it:
The
repeatmethod on the String object is "new" since Java 11.