Java uses a For loop to output a hollow diamond instance code _java

Source: Internet
Author: User
Tags int size

Write a program that outputs a hollow diamond on the console and a diagonal distance of 6.

Copy Code code as follows:

public class Diamond {
public static void Main (string[] args) {
Printhollowrhombus (6);
}

public static void Printhollowrhombus (int size) {
if (size% 2 = 0) {
size++;//Calculate 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 blank in upper left corner
}
for (int j = 0; J < 2 * i + 1; j + +) {
if (j = = 0 | | | j = = 2 * i) {
System.out.print ("*");/output diamond upper half Edge
} else {
System.out.print ("");/output diamond Upper half Hollow
}
}
System.out.println (""); Line Wrap
}
for (int i = SIZE/2 + 1; i < size; i++) {
for (int j = 0; J < I-size/2; J + +) {
System.out.print ("")//output diamond bottom left corner blank
}
for (int j = 0; J < 2 * size-1-2 * i; j +) {
if (j = = 0 | | | j = = 2 * (Size-i-1)) {
System.out.print ("*");/output diamond bottom half edge
} else {
System.out.print ("");//output diamond bottom half hollow
}
}
System.out.println (""); Line Wrap
}
}
}

Effect as shown:

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.