Java internal and external loop print ladder * number

Source: Internet
Author: User

A. Straight Ladder * Print

First on the result map

Idea: The basic constituent unit of the result diagram is an * number, which is implemented by internal and external for loop, the code is as follows:

1 classDayin2 {3      Public Static voidMain (string[] args)4     {5          for(intx=4;x>=0;x--)6         {7              for(inty=5; (y-x) >0;y--)8             {9System.out.print ("*");Ten             } One System.out.println (); A         } -     } -}

Two. Inverted Ladder printing

Directly on the code:

1 classDayin2 {3      Public Static voidMain (string[] args)4     {5          for(intx=0;x<5;x++)6         {7              for(inty=5; (y-x) >0;y--)8             {9System.out.print ("*");Ten             } One System.out.println (); A         } -     } -}

By adding System.out.println () in the outer loop; To achieve skip.

Summary: It will be found that the outer loop control is the number of rows, the inner loop control is the number of columns (number), then naturally, the outer loop just let it cycle 5 times,

The key point is the loop condition within the loop, adjusting the relationship of the row variable and the column variable to achieve the goal, the advantage will be good.

Java internal and external loop print ladder * number

Related Article

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.