Input n to print the triangle (for example, n = 3, n = 4, n = 5) 1 <= n <= 26

Source: Internet
Author: User

Package demo; public class printdemo {public static void main (string [] ARGs) {print (26);} Private Static void print (int I) {if (I <1 | I> 26) {return;} Int J, J2; For (j = 1; j <= I; j ++) {for (J2 = 1; J2 <= I; J2 ++) {If (J + J2 <= I) {system. out. print ("");} else {system. out. print (char) (64 + J2) ;}for (J2 = I-1; J2> = 1; J2 --) {If (J + J2> I) {system. out. print (char) (64 + J2);} else {system. out. print ("") ;}} system. out. println ();}}}
Output result:


Three points:

1. Do not write system. Out. Print () as system. Out. println.

2. If the operation is performed on the diagonal line, use if to determine.

3. Generally, two-tier loops control the number of rows in the outer loop. External Loop variables must exist in the internal loop!

Input N, printed triangle (for example, n = 3, n = 4, n = 5) 1 <= n <= 26

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.