For loop output hollow diamond shape "java"

Source: Internet
Author: User

Use the FOR Loop statement to output the following "hollow Diamond" effect:

    *   * *  *   * *     **       * *     *   *   *   * *    *

Recommended priority reference to the author of another article: "For loop output diamond shape" Java "

Code:

1 /*2     *3    * *4   *   *5  *     *6 *       *7  *     * 8   *   *9    * *Ten     * One  A Analysis: - shaped in a solid diamond -     * the    *** -   ***** -  ******* - ********* +  *******  -   ***** +    *** A     * at  - on the basis of a diamond pattern, the first and last stars of each row are output, and the other stars are output with a space.  - */ -  -  -  Public classStartree in { -      Public Static voidMain (string[] args) { to         intLayer = 5; +  -         //first, upper layer (show layer row) the          for(inti=1;i<=layer;i++){ *               $             //1, to get a space to display the numberPanax Notoginseng             intSpacenum = layer-i; -  the              for(intj=1;j<=spacenum;j++) {//the number of spaces that need to be printed +System.out.print (""); A             } the              +             //2. Get the number of stars to show -             intStarnum = 2*i-1; $  $              for(intj=1;j<=starnum;j++) {//number of prints per line required -                 if(j==1| | J==starnum) {//print the first and last stars in each line -System.out.print ("*"); the}Else{//each line is not the first and the last star to print into a space -System.out.print ("");Wuyi                 } the             } -             //3. Line break Wu System.out.println (); -         } About          $         //second, lower level -          for(intI= (layer-1); i>=1;i--){ -  -             intSapcenum = Layer-i;//gets the relationship between the number of spaces shown in each row and the number of rows I A              for(intj=1;j<=sapcenum;j++){ +System.out.print (""); the             } -  $             intStarnum = 2*i-1;//gets the relationship between the number of stars displayed in each row and the number of rows I the              for(intj=1;j<=starnum;j++){ the                 if(j==1| | J==starnum) {//print the first and last stars in each line theSystem.out.print ("*"); the}Else{//each line is not the first and the last star to print into a space -System.out.print (""); in                 } the             } the System.out.println (); About         } the     } the}

The Code results show:

For loop output hollow diamond shape "java"

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.