Use the FOR Loop statement to output the following funnel effect:
+------+|\..../|| \.. / || \/ || /\ || /.. \ || /....\|+------+
Code: (See the code header for more lines of comment)
In this example code, a constant height is used, which refers to half of the interior height value of the funnel as a constant value.
/** +------+ height:3 * |\..../| First line: Number of spaces: 0 Pips: 4 spaces: 0 * | \.. / | Second line: Number of spaces: 1 pips: 2 spaces: 1 * | \/ | Third line: Number of spaces: 2 pips: 0 Spaces: 2 * | /\ | * | /.. \ | * |/....\| * +------+ * * +--------+ height:4 * |\....../| First line: Number of spaces: 0 Pips: 6 spaces: 0 * | \..../ | Second line: Number of spaces: 1 pips: 4 spaces: 1 * | \.. / | Third line: Number of spaces: 2 pips: 2 spaces: 2 * | \/ | Line four: Number of spaces: 3 pips: 0 Spaces: 3 * | /\ | * | /.. \ | * | /....\ | * |/......\| * +--------+ * * Analysis: * 1, the graph is divided into three parts: *-the top and bottom of the graph; *-the upper part of the funnel *-the lower part of the funnel * 2, the upper part of the funnel is a fixed height * -Top and bottom graphics: * "-" Quantity: 2*height *-Funnel Upper part * Nth line: Number of spaces: line-1 points: (height-line Number of spaces: line-1 *-bottom of funnel * Nth line: Number of spaces: Height-line points: (line-1) Number of spaces: Height-line * * */ Public classTest { Public Static Final intHEIGHT = 3; Public Static voidMain (string[] args) {drawLine (); Drawtop (); Drawbottom (); DrawLine (); } Public Static voidDrawtop () { for(intline=1;line<=height;line++) {System.out.print ("|"); //output to the right of the space "" for(intI=1;i<= (line-1); i++) {System.out.print (" "); } System.out.print ("\\"); //output point "." for(inti=1;i<=2* (height-line); i++) {System.out.print ("."); } System.out.print ("/"); //output to the right of the space "" for(inti=1;i<=line-1;i++) {System.out.print (" "); } System.out.println ("|"); } } Public Static voidDrawbottom () { for(intline=height;line>0;line--) {System.out.print ("|"); //output to the right of the space "" for(intI=1;i<= (line-1); i++) {System.out.print (" "); } System.out.print ("/"); //output point "." for(inti=1;i<=2* (height-line); i++) {System.out.print ("."); } System.out.print ("\\"); //output to the right of the space "" for(inti=1;i<=line-1;i++) {System.out.print (" "); } System.out.println ("|"); } } Public Static voidDrawLine () {System.out.print ("+"); for(inti=1;i<=2*height;i++) {System.out.print ("-"); } System.out.println ("+"); }}
The Code results show:
For loop output funnel shape "Java"