需求:1、使用for循环实现九九乘法表正向和颠倒打印。2、使用while循环实现九九乘法表的正向打印和颠倒打印。
public class Print99Demo {
public static void main(String[] args) {
printFor99();
printWhile99();
printForReverse99();
printWhileReverse99();
}
/**
* for循环实现九九乘法表打印
*/
public static void printFor99() {
for (int x = 1; x <= 9; x++) {// x代表行号
for (int y = 1; y <= x; y++) {// y代表列号
System.out.print(y + "*" + x + "=" + y * x + "\t");
}
System.out.println();
}
}
/**
* while循环实现九九乘法表打印
*/
public static void printWhile99() {
int x, y;
x = 1;
while (x <= 9) {
y = 1;
while (y <= x) {
System.out.print(y + "*" + x + "=" + y * x + "\t");
y++;
}
System.out.println();
x++;
}
}
/**
* for循环实现九九乘法表颠倒打印
*/
public static void printForReverse99() {
for (int x = 9; x >= 1; x--) {
for (int y = 1; y <= x; y++) {
System.out.print(y + "*" + x + "=" + y * x + "\t");
}
System.out.println();
}
}
/**
* while循环实现九九乘法表颠倒打印
*/
public static void printWhileReverse99() {
int x, y;
x = 9;
while (x >= 1) {
y = 1;
while (y <= x) {
System.out.print(y + "*" + x + "=" + y * x + "\t");
y++;
}
System.out.println();
x--;
}
}
}