用java语言写一个二维数组输出倒三角星号矩阵
时间: 2023-09-16 16:10:08 浏览: 102
java二维数组输出下三角矩阵
5星 · 资源好评率100%
以下是用Java语言编写的输出倒三角星号矩阵的代码:
```
public class InvertedStarMatrix {
public static void main(String[] args) {
int[][] matrix = new int[5][5];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = 1;
}
}
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
if (i + j >= matrix.length) {
matrix[i][j] = 0;
}
}
}
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] == 1) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
```
输出结果如下:
```
* * * * *
* * * *
* * *
*
```
阅读全文