用Java语言的for循环写出10种99乘法表的不同输出样式
时间: 2023-11-27 10:53:50 浏览: 77
Java九九乘法表优化例子
1. 普通乘法表输出样式:
```
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " * " + i + " = " + (i * j) + " ");
}
System.out.println();
}
```
2. 带对齐的乘法表输出样式:
```
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.printf("%d * %d = %-2d ", j, i, (i * j));
}
System.out.println();
}
```
3. 倒序输出乘法表样式:
```
for (int i = 9; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " * " + i + " = " + (i * j) + " ");
}
System.out.println();
}
```
4. 纵向输出乘法表样式:
```
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
if (j < i) {
System.out.print(" ");
} else {
System.out.printf("%d * %d = %-2d ", i, j, (i * j));
}
}
System.out.println();
}
```
5. 左对齐输出乘法表样式:
```
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.printf("%-2d * %-2d = %-3d", j, i, (i * j));
}
System.out.println();
}
```
6. 带边框的乘法表样式:
```
System.out.println("+------------------------------------------------------+");
for (int i = 1; i <= 9; i++) {
System.out.print("| ");
for (int j = 1; j <= i; j++) {
System.out.printf("%d * %d = %-2d ", j, i, (i * j));
}
for (int j = i + 1; j <= 9; j++) {
System.out.print(" ");
}
System.out.println(" |");
}
System.out.println("+------------------------------------------------------+");
```
7. 带颜色的乘法表样式:
```
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
int result = i * j;
if (result < 10) {
System.out.printf("\u001B[31m%d * %d = %d \u001B[0m", j, i, result);
} else if (result >= 10 && result < 50) {
System.out.printf("\u001B[32m%d * %d = %d \u001B[0m", j, i, result);
} else {
System.out.printf("\u001B[34m%d * %d = %d \u001B[0m", j, i, result);
}
}
System.out.println();
}
```
8. 带空格的乘法表样式:
```
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
int result = i * j;
String space = " ";
if (result < 10) {
space += " ";
} else if (result >= 10 && result < 100) {
space += " ";
}
System.out.printf("%d * %d = %d%s", j, i, result, space);
}
System.out.println();
}
```
9. 带斜线的乘法表样式:
```
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.printf("%d\\%d=%-2d ", j, i, (i * j));
}
System.out.println();
}
```
10. 带加号的乘法表样式:
```
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.printf("%d + %d = %-2d ", j, i, (i + j));
}
System.out.println();
}
```
阅读全文