使用 for 循环编写一个 Java 程序来打印直角三角形中的连续数字列。
import java.util.Scanner;
public class ConsecutiveNumRightTri1 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Consecutive Numbers Right Triangle Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Consecutive Numbers Right Triangle Pattern");
int k = 1;
for (int i = 1 ; i <= rows; i++ )
{
for (int j = 1 ; j <= i; j++ )
{
System.out.print(k++ + " ");
}
System.out.println();
}
}
}

使用 while 循环的 Java 连续列数字直角三角形程序。
import java.util.Scanner;
public class ConsecutiveNumRightTri2 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Consecutive Numbers Right Triangle Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Consecutive Numbers Right Triangle Pattern");
int j, i = 1, k = 1;
while (i <= rows )
{
j = 1 ;
while( j <= i )
{
System.out.print(k++ + " ");
j++;
}
System.out.println();
i++;
}
}
}
Consecutive Numbers Right Triangle Pattern Rows = 10
Printing Consecutive Numbers Right Triangle Pattern
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
Java 程序使用 do while 循环打印连续数字的直角三角形列。
import java.util.Scanner;
public class ConsecutiveNumRightTri3 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Consecutive Numbers Right Triangle Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Consecutive Numbers Right Triangle Pattern");
int j, i = 1, k = 1;
do
{
j = 1 ;
do
{
System.out.print(k++ + " ");
} while( ++j <= i );
System.out.println();
} while (++i <= rows );
}
}
Consecutive Numbers Right Triangle Pattern Rows = 12
Printing Consecutive Numbers Right Triangle Pattern
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78