一 、使用 * 号
import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
5
*
**
***
****
*****
二 、使用数字
import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
if (j != i) {
System.out.print(j + " ");
} else {
System.out.print(j);
}
}
if (i != n) {
System.out.println();
}
}
}
}
5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
三 、使用字母
import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
char x = 'A';
for (int j = 1; j <= i; j++) {
if (j != i) {
System.out.print(x + " ");
x++;
} else {
System.out.print(x);
x++;
}
}
if (i != n) {
System.out.println();
}
}
}
}
5
A
A B
A B C
A B C D
A B C D E
四 、倒三角形
import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = n; i > 0; i--) {
for (int j = i; j > 0; j--) {
if (j != 1) {
System.out.print("* ");
} else {
System.out.print("*");
}
}
if (i != 1) {
System.out.println();
}
}
}
}
5
* * * * *
* * * *
* * *
* *
*
五 、倒三角形数字
import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = n; i > 0; i--) {
for (int j = i, k = 1; j > 0; j--, k++) {
if (j != 1) {
System.out.print(k + " ");
} else {
System.out.print(k);
}
}
if (i != 1) {
System.out.println();
}
}
}
}
5
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1