Use the FOR Loop statement to output the following "diamond" effect:
* *** ***** **************** ******* ***** *** *
Code
1 /*2 *3 ***4 *****5 *******6 *********7 ******* 8 *****9 ***Ten * One A Analysis: - -upper level (5 floors total) - composition per layer: number of spaces per layer + stars per layer the 1th Floor--Number of spaces: 4 + stars: 1 - 2nd Floor--Number of spaces: 3 + stars: 3 - 3rd Floor--Number of spaces: 2 + stars: 5 - 4th Floor--Number of spaces: 1 + stars: 7 + 5th Floor--Number of spaces: 0 + stars: 9 - + Analysis: A 1th Floor--Number of spaces: 4 (= 5-1) + stars: 1 (= 2*1-1) at 2nd Floor--Number of spaces: 3 (= 5-2) + stars: 3 (= 2*2-1) - 3rd Floor--Number of spaces: 2 (= 5-3) + stars: 5 (= 2*3-1) - 4th Floor--Number of spaces: 1 (= 5-4) + stars: 7 (= 2*4-1) - 5th Floor--Number of spaces: 0 (= 5-5) + Stars: 9 (= 2*5-1) - - Inference: (upper layer) in Layer I--(number of spaces = layer-i) + (number of stars = 2*i-1) - to + -lower level (lower layer of common layer-1) - 1th Floor--Number of spaces: 1 + stars: 7 the 2nd Floor--Number of spaces: 2 + stars: 5 * 3rd Floor--Number of spaces: 3 + stars: 3 $ 4th Floor--Number of spaces: 4 + stars: 1Panax Notoginseng - Analysis: Effect equivalent (total 4 layers) the the 1th layer on the lower level corresponds to the 4th floor of the upper layer-the number of spaces: 1 + stars: 7 + the 2nd layer on the lower level corresponds to the 3rd floor of the upper layer-the number of spaces: 2 + stars: 5 A the 3rd layer on the lower level corresponds to the 2nd floor of the upper layer-the number of spaces: 3 + stars: 3 the the 4th layer on the lower level corresponds to the 1th floor of the upper layer-the number of spaces: 4 + stars: 1 + - $ Reasoning: $ Layer I--(number of spaces = i) + (number of stars = layer-i)-1) - */ - the - Public classStartreeWuyi { the Public Static voidMain (string[] args) { - intLayer = 5; Wu - //first, upper layer (show layer row) About for(inti=1;i<=layer;i++){ $ - //1, to get a space to display the number - intSpacenum = layer-i; - for(intj=1;j<=spacenum;j++) {//the number of spaces that need to be printed ASystem.out.print (""); + } the - //2. Get the number of stars to show $ intStarnum = 2*i-1; the for(intj=1;j<=starnum;j++) {//number of stars to print theSystem.out.print ("*"); the } the //3. Line break - System.out.println (); in } the the //second, lower level (show Layer-1 line) About for(inti=1;i<layer;i++){ the the //1, to get a space to display the number the intSpacenum =i; + for(intj=1;j<=spacenum;j++){ -System.out.print (""); the }Bayi //2. Get the number of stars to show the intStarnum = (layer-i)-1; the for(intj=1;j<=starnum;j++){ -System.out.print ("*"); - } the //3. Line break the System.out.println (); the the } - } the}
The Code results show:
For loop output diamond shape "Java"