本次使用循环语句的三种写法写出九九乘法口诀表,实现效果如下图1所示:
1.第1种方法(for的写法):
public class NineNine {
public static void main(String[] args) {
System.out.println("使用for方法实现打印九九乘法口诀表");
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " * " + i + " = " + i * j + '\t');
}
System.out.print("\n");
}
}
}
2.第2种方法(while的写法):
public class NineNine {
public static void main(String[] args) {
System.out.println("使用while方法实现打印九九乘法口诀表");
int i = 1;
while (i <= 9) {
int j = 1;
while (j <= i) {
System.out.print(j + " * " + i + " = " + i * j + '\t');
j++;
}
System.out.print("\n");
i++;
}
}
}
3.第3种方法(do...while的写法):
public class NineNine {
public static void main(String[] args) {
System.out.println("使用do ... while方法实现打印九九乘法口诀表");
i = 1;
do {
int j = 1;
do {
System.out.print(j + " * " + i + " = " + i * j + '\t');
j++;
} while (j <= i);
System.out.print("\n");
i++;
} while (i <= 9);
}
}