Java uses for loop output Yang Hui's triangle _java

Source: Internet
Author: User

The idea is to create an integer two-dimensional array containing 10 one-dimensional arrays. Uses a two-tier loop to initialize the size of each second-tier array in the outer loop. In the inner loop, you assign the array elements on either side to 1, the other values are calculated by the formula, and then the array elements are output.

Copy Code code as follows:

public class Yanghuitriangle {
public static void Main (string[] args) {
int triangle[][]=new int[10][];//Create a two-dimensional array
Traversing the first layer of a two-dimensional array
for (int i = 0; i < triangle.length; i++) {
Triangle[i]=new int[i+1];//Initializes the size of the second-tier array
Traversing the second-tier array
for (int j=0;j<=i;j++) {
Assign the array elements on either side to 1
if (i==0| | j==0| | J==i) {
Triangle[i][j]=1;
}else{//other numerical values are calculated by formula
TRIANGLE[I][J]=TRIANGLE[I-1][J]+TRIANGLE[I-1][J-1];
}
System.out.print (triangle[i][j]+ "T"); Output array element
}
System.out.println (); Line Wrap
}
}
}

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.