A diamond pattern implemented with a for loop

Source: Internet
Author: User

 Packagecom.wh.lingxing;ImportJava.util.Scanner; Public classlingxing { Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in);  for (;;) {System.out.println ("Please enter the number:"); intnum =Sc.nextint (); intnum2 = 0; if(num% 2 = = 0) {//when NUM is evennum2 = NUM/2; //Upper part of Diamond                 for(inti = 1; I <= num2; i++) {                     for(intj = num2-i; J > 0; j--) {System.out.print (" "); }                     for(intK = 1; K <= 2 * i-1; k++) {System.out.print ("*"); } System.out.print ("\ n"); }                //the lower half of the diamond                 for(inti = 1; i < num2; i++) {                     for(intj = 1; J <= I; J + +) {System.out.print (" "); }                     for(intK = 1; K <= num-1-2 * i; k++) {System.out.print ("*"); } System.out.print ("\ n"); }            } Else{//when num is oddnum2 = (num + 1)/2; //Upper part of Diamond                 for(inti = 1; I <= num2; i++) {                     for(intj = num2-i; J > 0; j--) {System.out.print (" "); }                     for(intK = 1; K <= 2 * i-1; k++) {System.out.print ("*"); } System.out.print ("\ n"); }                //the lower half of the diamond                 for(inti = 1; i < num2; i++) {                     for(intj = 1; J <= I; J + +) {System.out.print (" "); }                     for(intK = 1; K <= num-1-2 * i; k++) {System.out.print ("*"); } System.out.print ("\ n"); }            }        }    }}

A diamond pattern implemented with a for loop

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.