Java uses the for loop to output hollow Diamond

Source: Internet
Author: User

Write a program and output the hollow diamond in the console. The diagonal distance is 6.

Public class Diamond {public static void main (String [] args) {printHollowRhombus (6);} public static void printHollowRhombus (int size) {if (size % 2 = 0) {size ++; // calculate the diamond size} for (int I = 0; I <size/2 + 1; I ++) {for (int j = size/2 + 1; j> I + 1; j --) {System. out. print (""); // output the blank position in the upper left corner} for (int j = 0; j <2 * I + 1; j ++) {if (j = 0 | j = 2 * I) {System. out. print ("*"); // output the upper half edge of the diamond} else {System. out. print (""); // output the upper half of the diamond hollow} System. out. println (""); // line feed} for (int I = size/2 + 1; I <size; I ++) {for (int j = 0; j <I-size/2; j ++) {System. out. print (""); // the white space in the lower left corner of the output diamond} for (int j = 0; j <2 * size-1-2 * I; j ++) {if (j = 0 | j = 2 * (size-I-1) {System. out. print ("*"); // output the lower half edge of the diamond} else {System. out. print (""); // output the lower half of the diamond hollow} System. out. println (""); // line feed }}}

Effect

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.