九九乘法表大家都熟悉,但是大家有没有想过它的各种表现形式呢
代码:
左下:
for (int m = 1; m <= 9; m++) {
for (int n = 1; n <= m; n++) {
System.out.print(n + " * " + m + " = " + m * n + "\t");
}
System.out.println();
}
左上:
for (int m = 9; m >= 1; m–) {
for (int n = m; n <= 9 && n >= 1; n--) {
System.out.print(m + " * " + n + " = " + m * n + "\t");
}
System.out.println();
}
右上:
for (int m = 9; m >= 1; m–) {
int a = 9-m;
while(a-->0)
System.out.print(" \t\t\t");
for (int n = m; n <= 9 && n >= 1; n--) {
System.out.print(m + " * " + n + " = " + m * n + "\t");
}
System.out.println();
}
右下:
for (int m = 1; m <= 9; m++) {
int a = 9-m;
while(a-->0)
System.out.print(" \t\t\t");
for (int n = 1; n <= m ; n++) {
System.out.print(m + " * " + n + " = " + m * n + "\t");
}
System.out.println();
}
正等腰:
for (int m = 1; m <= 9; m++) {
int a = (27-3*m)/2+1;
while(a-->0)
System.out.print(" \t");
for (int n = 1; n <= m ; n++) {
System.out.print(m + " * " + n + " = " + m * n + "\t");
}
System.out.println();
}
反等腰:
for (int m = 9; m >= 1; m–) {
int a = (27-3*m)/2+1;
while(a-->0)
System.out.print(" \t");
for (int n = m; n <= 9&&n>=1 ; n--) {
System.out.print(m + " * " + n + " = " + m * n + "\t");
}
System.out.println();
}