Print various graphics through Java: rectangle, triangle, 99 times table, diamond, Yang Hui triangle
1 Print rectangle
Any rectangle with n rows and m columns
* * * * * * * * * * * * * * * * * * * * * * * *Copy the code
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
int j = scanner.nextInt();
for (int k = 0; k < i; k++) {
for (int l = 0; l < j; l++) {
System.out.print("*\t");
}
System.out.println();
}
Copy the code
Print right triangles
Any upper right triangle, lower right triangle with n rows, for example:
*
**
***
****
*****
Copy the code
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
for (int k = 0; k < i; k++) {
for (int j = 0; j < k + 1; j++) {
System.out.print("*\t");
}
System.out.println();
}
Copy the code
Print right triangle 2
The following figure shows the number of rows with n
* * * * * * * * * * * * * * * *Copy the code
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
for (int k = 0; k < i; k++) {
for (int j = 0; j < k*2 + 1; j++) {
System.out.print("*");
}
System.out.println();
}
Copy the code
Print 99 times tables
Use “\t” to beautify the format, indicating that the empty space of a TAB is occupied.
for (int i = 1; i < 10; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + (j * i) + "\t");
}
System.out.println();
}
Copy the code
4 print diamond
The number of rows is n, odd rows
Scanner scanner = new Scanner(System.in); Int I = scanner. NextInt (); for (int j = 0; j < i; For (int k = 0; k < Math.abs(i / 2 - j); k++) { System.out.print(" "); } // * for (int k = 0; k < i - 2 * Math.abs(i / 2 - j); k++) { System.out.print("*"); } System.out.println(); }Copy the code
5 print hollow diamond
The number of rows is n, odd rows
@Test public void test10() { Scanner scanner = new Scanner(System.in); Int I = scanner. NextInt (); for (int j = 0; j < i; For (int k = 0; k < Math.abs(i / 2 - j); k++) { System.out.print(" "); } for (int k = 0; k < i - 2 * Math.abs(i / 2 - j); k++) { if (k == 0 || k == i - 2 * Math.abs(i / 2 - j) - 1) { System.out.print("*"); } else { System.out.print(" "); } } System.out.println(); }}Copy the code
Print Yang Hui triangle
Tip: Use two-dimensional arrays
1 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1.........Copy the code
Methods a
Scanner scanner = new Scanner(System.in); int i = scanner.nextInt(); // Create an outer array. Int [][] arr1 = new int[I][]; For (int j = 1; j <= i; Int [] arr2 = new int[j]; Arr1 [j-1] = arR2; arr1[j-1] = arR2; For (int i1 = 0; int i1 = 0; i1 < arr2.length; I1 ++) {if (i1 == 0) {// arR2 [i1] = 1; } else if (i1 == arr2.length - 1) {// arR2 [i1] = 1; } else {arR2 [i1] = arR1 [j-2][i1-1] + arr1[j-2][i1];} else {arR2 [i1] = arR1 [j-2][i1]; }}} for (int[] ints: arr1) {for (int anInt: ints) {system.out.print (anInt); } System.out.println(); }Copy the code
Method 2
Scanner sc = new Scanner(System.in); int i1 = sc.nextInt(); int[][] arr = new int[i1][i1]; for (int i = 0; i < arr.length; i++) { arr[i][0] = 1; arr[i][i] = 1; } for (int i = 2; i < arr.length; i++) { for (int j = 1; j < i; j++) { arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j]; }} for (int I = 0; i < arr.length; i++) { for (int j = 0; j <= i; j++) { System.out.print(arr[i][j] + "\t"); } System.out.println(); }Copy the code