Web-android Engineer -4-14 multiple loops of Java loop statements

Source: Internet
Author: User

Source: http://www.imooc.com/code/1497

The structure of a loop body that contains a looping statement is called a multiple loop . Three loop statements can be nested within themselves or nested within each other, the most common is the double cycle . In a double loop, the outer loop executes once, and the inner loop executes a circle.

As shown below:

For example: use * to print rectangles:

The implementation code is:

Execution Flow: When i = 1 o'clock, the outer loop condition is established, enters the inner loop, and begins to print the first line of content. At this point, J starts at 1, loops 8 times, wraps at the end of the inner loop, and achieves the output of the first row of 8 *. Next, return the outer loop I to 2, prepare to print the next line, and so on, until the rectangle is finished printing.

Task

Can you handle the double cycle? Test First ~ ~ ~ ~

Implementation function: Use * print right triangle, the effect is as follows:

Implementation ideas: In the above figure, the 1th line has an *, the 2nd line has two *, the 3rd line has three * number, so you can use a double loop to achieve. where the outer loop controls the number of rows, and the inner loop controls the * numbers of each row. At the same time, we find a rule that when each line of information is printed, the maximum value of the inner loop variable is equal to the value of the outer loop variable.

Please add the 7th and 11 lines of the editor to the complete statement.

1  Public classHelloWorld {2      Public Static voidMain (string[] args) {3         4System.out.println ("Print right triangle");5         6         //Outer loop control number of rows7          for(inti = 1; ) {8             9             //Inner Loop controls the * numbers per lineTen             //the maximum value of the inner loop variable is equal to the value of the outer loop variable One              for(intj = 1; ) { A                  -System.out.print ("*"); -             } the              -             //line wrapping after each line is printed - System.out.println (); -         } +     } -}

1  Public classHelloWorld {2      Public Static voidMain (string[] args) {3         4System.out.println ("Print right triangle");5         6         //Outer loop control number of rows7          for(inti = 1; I <= 3; i++) {8             9             //Inner Loop controls the * numbers per lineTen             //the maximum value of the inner loop variable is equal to the value of the outer loop variable One              for(intj = 1; J <= I; J + +) { A                  -System.out.print ("*"); -             } the              -             //line wrapping after each line is printed - System.out.println (); -         } +     } -}

Web-android Engineer -4-14 multiple loops of Java loop statements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.