# java foundation -- circular printing

Posted by DjMikeWatt on Fri, 31 Jan 2020 23:36:50 +0100

## java basic printing graphics

Output the solid diamond and the hollow diamond in the console, and the output results are as follows:

```        /*
*    *
*   ***
*  *****
* *******
*  *****
*   ***
*    *
*/```
```        /*
*    *
*   * *
*  *   *
* *     *
*  *   *
*   * *
*    *
*/```

Black Diamond code part:

```for(int i=1;i<=4;i++) {//First, control and hit the inverted triangle
for(int j= 1;j<=4-i;j++) {//Inner layer controls the number of each layer
System.out.print(" ");
/*
* 111
* 11
* 1
*/
}
for(int k= 1;k<=2*i-1;k++) {
/*
* The normal output graph here is:
*  *
*  ***
*  *****
*  *******
*
*/
System.out.print("*");//Normal printing here * 1 --- 3 --- 5 --- 7
}
System.out.println();
/*
* Splice with the previous output figure, and the space is represented by 1
* 111*
* 11***
* 1*****
* *******
* Then complete the diamond of the top half
*/
}
//The principle of the lower part is the same as that of the upper part.
for(int l=1;l<=2;l++) {
for(int o =1;o<=l;o++) {
System.out.print(" ");
}
for(int p=1;p<=2*(2-l+1)-1;p++) {
System.out.print("*");
}
System.out.println();
}```

The hollow diamond is based on the solid diamond
Make a judgment on the output number at the same time to see whether it meets the last value of each line. If it meets the output, it does not meet the output space:
Code part:

```for(int i=1;i<=4;i++) {//First, control and hit the inverted triangle
for(int j= 1;j<=4-i;j++) {//Inner layer controls the number of each layer
System.out.print(" ");
/*
* 111
* 11
* 1
*/
}
for(int k= 1;k<=2*i-1;k++) {
if(k==1 || k==2*i-1) {//The judgment is made here to meet the output right angle
//The two sides of a triangle, the first column is an asterisk, and the last is an asterisk
System.out.print("*");//Normal printing here * 1 --- 3 --- 5
}else {
System.out.print(" ");
}

}
System.out.println();
}//Upper half part
for(int l=1;l<=3;l++) {
for(int o =1;o<=l;o++) {
System.out.print(" ");
}
for(int p=1;p<=2*(3-l+1)-1;p++) {
if(p==1 || p==2*(3-l+1)-1) {
System.out.print("*");
}else {
System.out.print(" ");
}

}
System.out.println();
}```

Finally, a general formula of image is written with array
When you use it, you can change the number of the array to the pattern you defined

```class Test {
public static void main(String[] args) {
System.out.println(printHeart("*"));
}
//The output of this code is i love java, surrounded by a heart.
private static String printHeart(String input) {

int[] array = { 0, 1, 0, 0, 0, 1, 0,
1, 0, 1, 0, 1, 0, 1,
1, 0, 0, 1, 0, 0, 1,
1, 4, 5, 2, 3, 4, 1,
0, 1, 0, 0, 0, 1, 0,
0, 0, 1, 0, 1, 0, 0,
0, 0, 0, 1, 0, 0, 0 };

StringBuffer sb = new StringBuffer();
for (int i = 0; i < array.length; i++) {
if (i % 7 == 0)
sb.append("\n");
if (array[i] == 0)
sb.append("   ");
else if (array[i] == 4)
sb.append(" ");
else if (array[i] == 5)
sb.append(" I ");
else if (array[i] == 2)
sb.append("Lvoe ");
else if (array[i] == 3)
sb.append("Java");
else
sb.append("  " + input);
}
return sb.toString();
}

}```

In the future, printing patterns is a direct application of the right method.

Topics: Java Asterisk