使用两个循环打印9*9乘法表是最简单的,只使用一个循环的话难度增加一点,一个循环都不使用的话,也可以做到。
我的方法是递归:总共使用了三个方法,两次递归。 使用递归来打印9*9乘法表,好像有一点小题大做了,我认为它的意义在于锻炼一下自己的算法吧,闲着无事对编程语言小小的消遣。
代码并不复杂,注释就不写了,直接贴代码:
public class UpGreat {
public static void main(String args[]) {
fun1(9, 1);
}
public static void fun1(int max, int start) {
if (start == max) {
fun2(start, 1);
return;
}
fun2(start, 1);
start++;
System.out.println();
fun1(max, start);
}
public static void fun2(int first, int second) {
if (first == second) {
fun3(first, second);
return;
}
fun3(first, second);
second++;
fun2(first, second);
}
public static void fun3(int i, int j) {
System.out.print(i + "*" + j + "=" + i * j + "\t");
return;
}
}
如果你有其他的解决办法,欢迎交流!