练习小代码1--java三种循环结构实现九九乘法表的输出for循环,while循环,do_while循环

for循环

public class MultiplyingChart {
	public static void main(String args[]) {
		int i, j;
		for (i = 1; i <= 9; i++) {//从一循环到九
			for (j = 1; j <= i; j++)//外层每1次循环里面就要循环i次i=1,j<=1, j 从1循环到 1 ; 输出 1*1 = 1i=2,j<=2,j从1 循环到 2 输出 1*2=1 2*2 =4
				System.out.print(i + "*" + j + "=" + i * j + " ");
			System.out.println();
		}

	}
}

while循环

public class MultiplyingChat2 {
	public static void main(String args[]) {
		int i = 1;
		while (i <= 9) {
			int j = 1;
			while (j <= i) {
				System.out.print(i + "*" + j + "=" + i * j + " ");
				j++;
			}
			System.out.println();
			i++;
		}
	}

}

do_while循环

public class MultplyingChart3 {
	public static void main(String args[]) {
		int i = 1;
		do {
			int j = 1;
			do {
				System.out.print(i + "*" + j + "=" + i * j + " ");
				j++;
			} while (j <= i);
			System.out.println();
			i++;

		} while (i <= 9);
	}

}

输出结果如下
在这里插入图片描述

发布了36 篇原创文章 · 获赞 61 · 访问量 2889

猜你喜欢

转载自blog.csdn.net/Miracle1203/article/details/103261131