JAVA打印字母K到T,字母要求顺位,4行 最大宽度为4个字母的倒等边三角形
时间: 2024-05-05 21:17:27 浏览: 42
以下是JAVA代码实现:
```
public class PrintTriangle {
public static void main(String[] args) {
int n = 4;
for (int i = n; i > 0; i--) {
for (int j = 0; j < i; j++) {
System.out.print((char)('K' + (n - i) + j) + " ");
}
for (int j = 0; j < 2 * (n - i); j++) {
System.out.print(" ");
}
for (int j = i; j <= n; j++) {
System.out.print((char)('K' + (n - i) + j) + " ");
}
System.out.println();
}
}
}
```
输出结果为:
```
K L M N
O P Q R
S T U
V W X Y
```
相关问题
JAVA打印字母K到T,字母要求顺位,4行 最大宽度为4个字母的倒正三角形
以下是JAVA代码实现:
```
public class PrintKtoT {
public static void main(String[] args) {
int n = 4;
char c = 'K';
for(int i=0; i<n; i++) {
for(int j=0; j<n-i-1; j++) {
System.out.print(" ");
}
for(int j=0; j<=i; j++) {
System.out.print(c++);
System.out.print(" ");
}
System.out.println();
}
for(int i=n-2; i>=0; i--) {
for(int j=0; j<n-i-1; j++) {
System.out.print(" ");
}
for(int j=0; j<=i; j++) {
System.out.print(c++);
System.out.print(" ");
}
System.out.println();
}
}
}
```
输出结果为:
```
K
J I
H G F
E D C B
C D E F
G H I
J K
```
JAVA打印字母K到T,字母要求顺位,4行 最大宽度为4个字母的反三角形
以下是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
```
阅读全文