Use the FOR Loop statement to output the following "hollow Diamond" effect:
* * * * * * ** * * * * * * * *
Recommended priority reference to the author of another article: "For loop output diamond shape" Java "
Code:
1 /*2 *3 * *4 * *5 * *6 * *7 * * 8 * *9 * *Ten * One A Analysis: - shaped in a solid diamond - * the *** - ***** - ******* - ********* + ******* - ***** + *** A * at - on the basis of a diamond pattern, the first and last stars of each row are output, and the other stars are output with a space. - */ - - - Public classStartree in { - Public Static voidMain (string[] args) { to intLayer = 5; + - //first, upper layer (show layer row) the for(inti=1;i<=layer;i++){ * $ //1, to get a space to display the numberPanax Notoginseng intSpacenum = layer-i; - the for(intj=1;j<=spacenum;j++) {//the number of spaces that need to be printed +System.out.print (""); A } the + //2. Get the number of stars to show - intStarnum = 2*i-1; $ $ for(intj=1;j<=starnum;j++) {//number of prints per line required - if(j==1| | J==starnum) {//print the first and last stars in each line -System.out.print ("*"); the}Else{//each line is not the first and the last star to print into a space -System.out.print ("");Wuyi } the } - //3. Line break Wu System.out.println (); - } About $ //second, lower level - for(intI= (layer-1); i>=1;i--){ - - intSapcenum = Layer-i;//gets the relationship between the number of spaces shown in each row and the number of rows I A for(intj=1;j<=sapcenum;j++){ +System.out.print (""); the } - $ intStarnum = 2*i-1;//gets the relationship between the number of stars displayed in each row and the number of rows I the for(intj=1;j<=starnum;j++){ the if(j==1| | J==starnum) {//print the first and last stars in each line theSystem.out.print ("*"); the}Else{//each line is not the first and the last star to print into a space -System.out.print (""); in } the } the System.out.println (); About } the } the}
The Code results show:
For loop output hollow diamond shape "java"