Java implements the print triangle method, and java implements the print triangle

Source: Internet
Author: User

Java implements the print triangle method, and java implements the print triangle

The examples in this article share the code for printing the positive triangle in java for your reference. The specific content is as follows:

Code:

Package BasicType; /*** encapsulate a method that can print the positive triangle based on user input values * @ author Administrator */public class Enme {// n indicates the number of printed layers public static void print_positive_triangle (int n) {// The first layer is 1, the second layer is 3, and the third layer is 5... the n layer is the last * int last = 2 * (n-1) + 1; // controls the number of layers printed for (int I = 0; I <n; I ++) {// calculate the number of spaces to be filled on the left of each layer. int full_left = last/2-i; // print the layer and wrap the System. out. println (""); // controls the style to be printed on the current layer. The square for (int j = 0; j <= last; j ++) is printed by default) {// If j is less or equal than the number of spaces to be filled or j is greater than the sum of the number of positions occupied by X and the number of spaces to be filled, print the space if (j <= full_left | j> full_left + 2 * I + 1) {System. out. print ("");} else {System. out. print ("*") ;}}} public static void main (String [] args) {print_positive_triangle (5 );}}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.