Use the FOR Loop statement to output the following tree effect:
* *** ***** **************** * * * * *
Code:
/* * * * * * * * * * * * * **************** * * * * Analysis:-Tree Top (total 5 layers) per layer: each layer of space per layer + number of stars per layer 1th-empty Number of squares: 4 + stars: 1 2nd--Spaces: 3 + stars: 3 3rd--Number of spaces: 2 + Stars: 4 level 4--spaces: 1 + stars: 7 5th Layer--Number of spaces: 0 + stars: 9 Analysis: Layer 1th--spaces: 4 (= 5-1) + stars: 1 (= 2*1-1) 2nd Layer--spaces: 3 (= 5- 2) + stars: 3 (= 2*2-1) 3rd Floor--Number of spaces: 2 (= 5-3) + stars: 4 (= 2*3-1) 4th level--Number of spaces: 1 (= 5-4) + stars: 7 (= 2*4-1) 5th Floor--Number of spaces: 0 (= 5-5) + Stars: 9 (= 2*5-1) Reasoning: (Layer tree top) level I--number of spaces = (layer-i) + (number of stars = 2*i-1)- Trunk (total 5 layers) 1th floor--spaces: 4 + stars: 1 2nd--Spaces: 4 + stars: 1 3rd--Spaces: 4 + stars: 1 level 4-- Number of spaces: 4 + stars: 1 5th--Spaces: 4 + stars: 1 similar to the 1th result, i.e.: layer 1th--spaces: 4 + stars: 1 The result of the first layer of the analogy tree top Draw: 1th Floor--Number of spaces: (LAYER-1) + stars: 1 Analysis: (Layer tree top) 1th--Number of spaces: (LAYER-1) + stars: 1 2nd--Space number: (Lay ER-1) + number of stars: 13rd Floor--Number of spaces: (LAYER-1) + stars: 1 Layer 4--Number of spaces: (LAYER-1) + stars: 1 5th Floor--Number of spaces: (LAYER-1) + stars: 1 */ Public classstartree{ Public Static voidMain (string[] args) {intLayer = 5; //first, the tree top 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 printedSystem.out.print (""); } //2. Get the number of stars to show intStarnum = 2*i-1; for(intj=1;j<=starnum;j++) {//number of stars to printSystem.out.print ("*"); } //3. Line breakSystem.out.println (); } //second, the trunk for(inti=1;i<=layer;i++){ //1, to get a space to display the number intSpacenum = Layer-1; for(intj=1;j<=spacenum;j++) {System.out.print (" "); } //2. Get the number of stars to display and wrapSystem.out.println ("*"); } }}
The Code results show:
For loop output tree shape "java"