[Basic java Algorithm] print Diamond

Source: Internet
Author: User

First:

Public class Test1 {public static void main (String [] args) {int n = 11; // number of rows for (int I = 0; I <n; I ++) {if (I <= (n-1)/2) {for (int j = I; j <(n-1)/2; j ++) {System. out. print ("") ;}for (int k = 0; k <2 * I + 1; k ++) {System. out. print ("*") ;}} else {for (int j = 0; j <I-(n-1)/2; j ++) {System. out. print ("") ;}for (int k = 0; k <2 * (n-I)-1; k ++) {System. out. print ("*") ;}} System. out. println ();}}}

Result:

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

Second:

Public class Test1 {public static void main (String [] args) {final int size = 5; // define the number of rows. // create a graph with 1-4 columns for (int I = 1; I <= size; I ++) {// Number of partitions in the outer loop control for (int j = I; j <size; j ++) {// This layer controls the space of the System. out. print ("") ;}for (int k = 1; k <= 2 * I-1; k ++) {// This layer controls the number of stars. System. out. print ("*");} System. out. println () ;}// print the image in the reverse direction, that is, the lower part of the image for (int I = size-1; I> = 1; I --) {for (int j = 1; j <= size-I; j ++) {System. out. print ("") ;}for (int k = 2 * I-1; k> 0; k --) {System. out. print ("*");} System. out. println ();}}}

Result:

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


This article from the "CEO Road" blog, please be sure to keep this source http://zhaohaibo.blog.51cto.com/7808533/1287595

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.