普通for循环和递归算法实现九九乘法表

普通for:

	//普通for循环九九乘法表
	public static void main(String[] args) {

		for (int a = 1; a <= 9; a++) {

			for (int b = 1; b <= a; b++) {

				System.out.print(a + "*" + b + "=" + a * b + "\t");

			}

			System.out.println();

		}

	}

}

递归算法实现(两种):

public class Test10 {

	// 递归算法九九乘法表--第一种
	public static void sheet1(int i) {
		if (i == 1) {
			System.out.println("1*1=1");
		} else {
			sheet1(i - 1);
			for (int j = 1; j <= i; j++) {
				System.out.print(j + "*" + i + "=" + j * i + "\t");
			}
			System.out.println();
		}
	}

	// 递归算法九九乘法表--第二种
	public static void sheet2(int i) {
		for (int j = 1; j <= i; j++) {
			System.out.print(j + "*" + i + "=" + j * i + "\t");
		}
		if (i < 9) {
			i++;
			System.out.println();
			sheet2(i);
		}
	}

	public static void main(String[] args) {
		Test10.sheet1(9);
		System.out.println("==========");
		Test10.sheet2(1);
	}
}

你可能感兴趣的:(笔记第一期)