欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java三种循环结构实现九九乘法表的输出for循环,while循环,do_while循环

程序员文章站 2022-06-06 10:32:37
...

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);
	}

}

输出结果如下
java三种循环结构实现九九乘法表的输出for循环,while循环,do_while循环