public class OutputTriangle {
public static void main(String[] args) {
int rows = 4;//三角形的行数,这里也可以换成控制台输入
int max = rows*2-1;
for (int i = 1; i <= rows; i++) {
int star = 2*i-1; //打印星星的个数
int spaces = (max - star)/2; //打印空格的个数
for (int j = 0; j < spaces; j++) {
System.out.print(" ");
}
for (int j = 0; j < star; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
有问题可以再给我发
public static void main(String[] args) {
final int row = 4;
for(int i = 0 ;i < row; i++){
System.out.println(String.format("%"+row+"s",String.join("", Collections.nCopies(i+1, "*"))) + String.join("", Collections.nCopies(i, "*")));
}
}
```java
```
我在IDE里面试验了一下,直接打出好像不太对,看图片好像号之间有空格的。你看看这个是你要的吗
public class Triangle {
public static void main(String[] arg) {
int lineMax = 4;
int columnMax = 14;
for (int line = 1; line <= lineMax; line++) {
for (int column = 1; column <= columnMax; column++) {
if (((column + line * 2) < (columnMax / 2 +1))) {
System.out.print(" ");
continue;
}
if (column % 2 == 0) {
System.out.print(" ");
} else {
if(column>columnMax/2+line*2-1){
System.out.print(" ");
}else{
System.out.print("*");
}
}
}
System.out.println();
}
}
}
打印出来的结果是这样的,你可以调整lineMax 和columnMax 来调整三角形大小,比如调整成
5,18或6,22或7,26来调整三角形大小。多一行需要多加4列
*
* * *
* * * * *
* * * * * * *