A positive triangulation method in Java

Source: Internet
Author: User

In the practice of looping statements, it is a classic example to draw a positive triangle, but if the method is not the right one, the resulting code is very complex and not highly applied.

There are two ways to draw the positive triangle, the first is a more troublesome approach, is through the induction of each row and column and the required triangle of the intrinsic relationship obtained, more cumbersome

Package complementation;

public class Trangle {

public static void Main (string[] args) {
int row = 6;
for (int i = 1; I <= row; i++) {
int m = 1;
for (int j = 1; J <= 2 * row-1; j + +) {
if (j = = Row-i + M) {
System.out.print ("*");
M + = 2;
}
else{
System.out.print ("");
}
if (M > 2 * i-1) {
Break
}
}
System.out.println ();
}
}

The second method is to directly find the space and the number of lines given to the relationship between the asterisk and the number of rows of the link, the method is very simple, practical, should be summed up:

Package complementation;

public class Easyer {
public static void Main (string[] args) {
int row = 5;
for (int i = 0; i < row; i++) {
for (int j = 0; J < Row-i; J + +) {
System.out.print ("");
}
for (int j = 0; J <= I; j + +) {
System.out.print ("*");
}
System.out.println ();
}
}

}

A positive triangulation method in Java

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.