Use JAVA to print edges, and use JAVA to print edges.

Source: Internet
Author: User

Use JAVA to print edges, and use JAVA to print edges.

Public class Test {public static void main (String [] args) {// print (5);} public static void print (int sum) {int number = 0; for (int I = 1; I <= sum; I ++) {int emptyNumber = sum-I; // calculate the number of spaces after calculation is the total number of rows-number of rows int starNumber = 2 * I-1; // calculate the number of stars, after calculation is 2 times the number of rows-1 if (I = sum) {number = I + starNumber; // save a total number for the back-end output} for (int z = 0; z <emptyNumber; z ++) // traverse the output, obtain a triangle {System. out. print ("") ;}for (int j = 0; j <starNumber; j ++) {System. out. print ("*");} System. out. println () ;}// start to output the following part of int newrow = sum + 1; // calculate the number of rows starting with int sumrow = 2 * sum-1; // calculate the total number of new rows for (int I = newrow; I <= sumrow; I ++) {int emptyNumber = I-sum; // calculate the number of spaces, calculated as the current number of rows-the old total number of rows // calculates the number of stars, calculated by the number of stars + space + number of rows = number, that is, the number of stars in the maximum triangle + its number of rows int starNumber = number-I-emptyNumber; // traverse the output for (int z = 0; z <emptyNumber; z ++) {System. out. print ("") ;}for (int j = 0; j <starNumber; j ++) {System. out. print ("*");} System. out. println ();}}}

Result chart:

 

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.