JAVA打印字母K到T,字母要求顺位,4行 最大宽度为4个字母的反三角形
时间: 2024-05-03 17:23:28 浏览: 31
以下是JAVA代码实现:
```
public class PrintingKToT {
public static void main(String[] args) {
int maxCols = 4;
int currentRow = 1;
int currentCol = maxCols;
char currentChar = 'K';
while (currentRow <= maxCols) {
int colsToPrint = currentRow <= maxCols / 2 ? currentRow : maxCols - currentRow + 1;
for (int i = 1; i <= colsToPrint; i++) {
System.out.print(currentChar + " ");
currentChar++;
}
for (int i = colsToPrint + 1; i <= maxCols; i++) {
System.out.print(" ");
}
System.out.println();
if (currentRow < maxCols / 2 + 1) {
currentCol--;
} else {
currentCol++;
}
currentRow++;
currentChar = (char) ('K' + currentCol * maxCols - (maxCols - currentRow));
}
}
}
```
运行结果如下:
```
K
J L
I M
H G F E D C B A T
```
阅读全文