循环语句三种(for,while,do...while)的使用方法

本次使用循环语句的三种写法写出九九乘法口诀表,实现效果如下图1所示:


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

    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容