# Java various pattern printing

2022-01-26 22:58:20

# 5*5 Square

We want to print 5*5 The square of , According to what we have learned before, we can understand this first

``````// Use single layer cyclic printing
for(int i = 1;i<=5;i++){

System.out.print("*****"+"\t");
}
``````

It can be seen that the effect can be printed with single-layer cycle , So we think what if the pattern to be printed is not the same for each column , At this time, we think of adding another layer of circulation

``````for(int i = 1;i<=5;i++){

for(int j = 1;j<=5;j++){

System.out.print("*"+"\t");
}
}
``````

A comparison of the first two found , The first layer of loop can control rows , The second control column
Running results :

# 99 Multiplication table

Go straight to the code

``````public class MultiplicationTables99{

public static void main(String[] args){

for(int i=1;i<=9;i++){
// That's ok
for(int j=1;j<=i;j++){
// Column
System.out.print(j+"*"+i+"="+i*j+"\t");// Format of multiplication table
}
System.out.println();// Output line feed
}
}
}
``````

Running results ：

Use a single-layer cycle to do

``````public class MultiplicationTables99{

public static void main(String[] args){

int j = 1;
for(int i=1;i<=9;i++){
// That's ok
System.out.print(j+"*"+i+"="+i*(j++)+"\t");// Format of multiplication table
if(j>i){
// It's equivalent to replacing the second layer of loop
j = 1;
i++;
System.out.println();
}
}
}
}
``````

# just （ pour ） Triangle

``````public class ZhengTriangle{

public static void main(String[] args){

for(int i=1;i<=5;i++){
// The control line
for(int k=5;k>i;k--){
// Print space
System.out.print(" ");
}
for(int j=1;j<=i;j++){
// Print "*"
System.out.print("*"+" ");
}
System.out.println();
}
}
}
``````

Running results ：

# Inverted triangle

``````public class DaoTriangle{

public static void main(String[] args){

for(int i=1;i<=5;i++){
// Print row
for(int k=1;k<i;k++){
// Can be replaced by "k--"
System.out.print(" ");// Print space
}
for(int j=5;j>=i;j--){

System.out.print("*"+" ");// Print "*"
}
System.out.println();
}
}
}
``````

Running results ：

# Yang hui triangle

The printing of Yang Hui triangle requires a two-dimensional array , First, we observe the law , It can be found that we can first determine the number of rows and columns , So define a two-dimensional array , And when i=j perhaps j=0 The time is 0, And the laws of other ranks meet arrys[i][j] = arrys[i-1][j]+arrys[i-1][j-1]; Then let it be

``````public class YangHui{

public static void main (String[] args){

// Create a row and column as 10 Two dimensional array of
int arrys[][] = new int [10][10];
// first floor for Loop controls the number of rows , The second layer controls the number of columns
for(int i = 0;i<arrys.length;i++){

for(int j = 0;j<i+1;j++){

// Observation shows that if i=j Hour or second 0 Column time , by 1
if(i==j || j == 0){

arrys[i][j] = 1;
}else {

// The other ranks follow the rules , Let this be the number one i That's ok , The first j Column , be
// It will be equal to (i-1,j),(i-1,j-1) The sum of the values corresponding to the subscript
arrys[i][j] = arrys[i-1][j]+arrys[i-1][j-1];
}
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}
}
``````

Running results ：