编写一个 Java 程序,使用 for 循环打印左侧帕斯卡数字金字塔。
import java.util.Scanner;
public class LeftPascalNumber1 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
int i, j, k;
System.out.print("Enter Left Pascals Number Triangle Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Left Pascals Triangle Number Pattern");
for (i = 1 ; i <= rows; i++ )
{
for (j = i ; j < rows; j++ )
{
System.out.print(" ");
}
for(k = 1; k <= i; k++)
{
System.out.print(k + " ");
}
System.out.println();
}
for (i = rows; i >= 1; i-- )
{
for (j = i ; j <= rows; j++ )
{
System.out.print(" ");
}
for(k = 1; k < i; k++)
{
System.out.print(k + " ");
}
System.out.println();
}
}
}

此 Java 示例 使用 while 循环显示左侧帕斯卡数字金字塔模式。
import java.util.Scanner;
public class LeftPascalNumber2 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Left Pascals Number Triangle Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Left Pascals Triangle Number Pattern");
int i = 1, j, k;
while (i <= rows )
{
j = i ;
while ( j < rows )
{
System.out.print(" ");
j++;
}
k = 1;
while( k <= i)
{
System.out.print(k + " ");
k++;
}
System.out.println();
i++;
}
i = rows;
while (i >= 1)
{
j = i ;
while (j <= rows)
{
System.out.print(" ");
j++;
}
k = 1;
while( k < i)
{
System.out.print(k + " ");
k++;
}
System.out.println();
i--;
}
}
}
Enter Left Pascals Number Triangle Pattern Rows = 7
Printing Left Pascals Triangle Number Pattern
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Java 使用 do while 循环打印左侧帕斯卡数字金字塔程序。
import java.util.Scanner;
public class LeftPascalNumber3 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Left Pascals Number Triangle Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Left Pascals Triangle Number Pattern");
int i = 1, j, k;
do
{
j = i ;
do
{
System.out.print(" ");
} while ( j++ < rows );
k = 1;
do
{
System.out.print(k + " ");
} while( ++k <= i);
System.out.println();
} while (++i <= rows );
i = rows;
do
{
j = i - 1;
do
{
System.out.print(" ");
} while (++j <= rows);
k = 1;
do
{
System.out.print(k + " ");
} while( ++k < i);
System.out.println();
} while (--i > 1) ;
}
}
Enter Left Pascals Number Triangle Pattern Rows = 9
Printing Left Pascals Triangle Number Pattern
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1