I am trying to display a diamond of asterisks using nested for
loops.
Here is my code so far:
public
int n = 9;
for(int i =0;i<n;i++){
for(int k=n-1;k>i;k--){
System.out.print(" ");
}
for(int j=0;j<2*i+1;j++){
System.out.print("*");
}
System.out.println("");
}
for(int j=0;j<n-1;j++){
for(int k=j;k>=0;k--){
System.out.print(" ");
}
for(int i=2*(n-j-1)-1;i>0;i--){
System.out.print("*");
}
System.out.println("");
}
You can print a diamond of asterisks (mathematical operators) with for
loops:
int m = 4;
int n = 4;
for (int i = -m; i <= m; i++) {
for (int j = -n; j <= n; j++) {
int val = Math.abs(i) + Math.abs(j);
System.out.print(val > Math.max(m, n) ? " " : "∗");
if (j < n) {
System.out.print(" ");
} else {
System.out.println();
}
}
}
Output:
∗
∗ ∗ ∗
∗ ∗ ∗ ∗ ∗
∗ ∗ ∗ ∗ ∗ ∗ ∗
∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗
∗ ∗ ∗ ∗ ∗ ∗ ∗
∗ ∗ ∗ ∗ ∗
∗ ∗ ∗
∗
You can create a 2d array with a diamond of asterisks using IntStreams:
int m = 4;
int n = 4;
String[][] arr = IntStream.rangeClosed(-m, m)
.mapToObj(i -> IntStream.rangeClosed(-n, n)
.map(j -> Math.abs(i) + Math.abs(j))
.mapToObj(j -> j > Math.max(m, n) ? " " : "∗")
.toArray(String[]::new))
.toArray(String[][]::new);
// formatted output
Arrays.stream(arr)
.map(row -> Arrays.stream(row)
.collect(Collectors.joining(" ", "[ ", " ]")))
.forEach(System.out::println);
[ ∗ ]
[ ∗ ∗ ∗ ]
[ ∗ ∗ ∗ ∗ ∗ ]
[ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ]
[ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ]
[ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ]
[ ∗ ∗ ∗ ∗ ∗ ]
[ ∗ ∗ ∗ ]
[ ∗ ]
See also:
• Empty diamond shape with numbers
• Filling a 2d array with numbers in a rhombus form
In your first for loop remove =
mark and just use <
e.g. for (int i = 1; i < size; i += 2)
.
Full code:
int size = 9;
for (int i = 1; i < size; i += 2) {
for (int k = size; k >= i; k -= 2) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}// end loop
for (int i = 1; i <= size; i += 2) {
for (int k = 1; k <= i; k += 2) {
System.out.print(" ");
}
for (int j = size; j >= i; j--) {
System.out.print("*");
}
System.out.println();
}// end loop
Try this code:
I have changed the first loop:
for (int i = 1; i <= size-1; i += 2) {
int size = 9;
for (int i = 1; i <= size - 1; i += 2) {
for (int k = size; k >= i; k -= 2) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}// end loop
for (int i = 1; i <= size; i += 2) {
for (int k = 1; k <= i; k += 2) {
System.out.print(" ");
}
for (int j = size; j >= i; j--) {
System.out.print("*");
}
System.out.println();
}// end loop
By using String#repeat, introduced as part of Java-11, you can do it with a single loop.
public class Main {
public static void main(String[] args) {
int size = 9;
int midRowNum = size / 2 + 1;
for (int i = 1 - midRowNum; i < midRowNum; i++) {
System.out.println(" ".repeat(Math.abs(i)) + "*".repeat((midRowNum - Math.abs(i)) * 2 - 1));
}
}
}
Output:
*
***
*****
*******
*********
*******
*****
***
*
By increasing the amount of space by one character, you can also print a variant of the diamond:
public class Main {
public static void main(String[] args) {
int size = 9;
int midRowNum = size / 2 + 1;
for (int i = 1 - midRowNum; i < midRowNum; i++) {
System.out.println(" ".repeat(Math.abs(i)) + "* ".repeat((midRowNum - Math.abs(i)) * 2 - 1));
}
}
}
Output:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Just for fun... :) try my code....
public class Diamond {
static String sp(int n) {
String s = "";
for (int i = 0; i < n; i++)
s += " ";
return s;
}
static String st(int n) {
String s = "";
for (int i = 0; i < n; i++)
s += "*";
return s;
}
static int abs(int n) {
if (n < 0)
return -n;
else
return n;
}
public static void main(String[] args) {
int size = 9;
for (int i = 0; i < size; i++) {
System.out.println(sp(abs((size - 1) / 2 - i)) +
st(abs(9 - 2 * ((i + 5) % (size)))) +
sp(abs((size - 1) / 2 - i)));
}
}
}