Package com.test;
Import Java.util.Scanner;
public class Test {
public static void Main (string[] args)//Create an object to invoke, implement console input and output, because the input is a character, so a.show call the object when the emphasis on conversion.
{
Yanghui a = new Yanghui ();
Scanner s = new Scanner (system.in);
System.out.println ("Please enter a number to represent the number of rows:");
String str = s.nextline ();
A.show (integer.valueof (str));
}
}
Class yanghui//object calls the classes name
{
public void Show (int n)
{
Int[][] s = new Int[n + 1][n + 1];//defines a two-dimensional array because it is n rows, so define n+1 to prevent overflow (loop starting from 1)
for (int x = 1; x <= n × x + +)//This cycle is used to control the number of rows
{
for (int z = 1; z <= n + 1-x; z++)//This small loop is used to control the number of spaces in front of each line, and the more backward and misaligned, adjust the
{
System.out.print ("");
}
for (int y = 1; y <= x; y++)//This loop controls the number of rows per line
{
if (y = = 1)//fixed the value of the first number per line, and assigned a two-dimensional array, and printed
{
System.out.print (y + "");
S[x][y] = 1;
}
else if (y = = x)//fixed the value of the last number per line and assigned a two-dimensional array and printed
{
System.out.print (1 + "");
S[x][y] = 1;
}
The else//is used to print out the intermediate values of the first and last of each line and give the two-dimensional array
{
System.out.print (S[x-1][y-1] + s[x-1][y] + "");//The median value of each row is added by the s[x-1][y-1]+s[x-1][y of the previous row and given a two-dimensional array
S[x][y] = S[x-1][y-1] + s[x-1][y]; Record the value and prepare for the next line
}
}
System.out.println ();//Line break
}
}
}
Yang Hui triangle _ a good example of a two-dimensional array (reproduced)