【Java】【基础篇】day03:循环语句与函数

前言

本期任务:毕向东老师Java视频教程学习笔记(共计25天)


一、打印三角形

/*
需求:打印三角形

思路;
    两重循环

步骤:
    1. 第一层循环控制三角形层数
    2. 第二层循环控制'*'的个数和位置
    3. 注意换行

思路;
    函数实现

步骤:返回值为空,参数可设置为输出行数
    1. 重载两种print99函数,一种指定输出行数,一种默认打印9行
    2. 主逻辑部分复制非函数方案


*/

public class PrintTriangle {
    public static void main(String[] args) {
        int n = 5;
        /*
            打印如下三角形:
                *
                * *
                * * *
                * * * *
                * * * * *
         */
        printTriangle90(n);
//        for (int x = 1; x <= n; x++) {
//            for (int y = 1; y <= x; y++) {
//                System.out.print("* ");
//            }
//            System.out.println();
//        }
//        System.out.println("--------------");

        /*
            打印如下三角形:
                    *
                   * *
                  * * *
                 * * * *
                * * * * *
         */
        printTriangle60(n);
//        for (int x = 1; x <= n; x++) {
//            for (int y = 1; y <= n - x; y++) {
//                System.out.print(" ");
//            }
//            for (int y = 1; y <= x; y++) {
//                System.out.print("* ");
//            }
//            System.out.println();
//        }

    }

    // 打印直角三角形阵
    public static void printTriangle90(int n) {
        for (int x = 1; x <= n; x++) {
            for (int y = 1; y <= x; y++) {
                System.out.print("* ");
            }
            System.out.println();
        }
        System.out.println("--------------");
    }

    // 打印等边三角形阵
    public static void printTriangle60(int n) {
        for (int x = 1; x <= n; x++) {
            for (int y = 1; y <= n - x; y++) {
                System.out.print(" ");
            }
            for (int y = 1; y <= x; y++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

二、打印九九乘法表

/*
需求:打印九九乘法表

思路;
    两重循环

步骤:
    1. 第一层循环控制乘法表行数
    2. 第二层循环控制公式个数和内容
    3. 注意换行

思路;
    函数实现

步骤:返回值为空,参数可设置为输出行数
    1. 重载两种print99函数,一种指定输出行数,一种默认打印9行
    2. 主逻辑部分复制非函数方案
*/

public class PrintMultiplicationTable {
    public static void main(String[] args) {
        System.out.println("=======================================================================");

//        int n = 9;
//        for (int i = 1; i <= n; i++) {
//            for (int j = 1; j <= i; j++) {
//                System.out.print(i + "*" + j + "=" + (i * j) + "\t");
//            }
//            System.out.println();
//        }
        print99();
        print99(8);

        System.out.println("=======================================================================");
    }

    // 打印指定行数的九九乘法表
    public static void print99(int n) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i + "*" + j + "=" + (i * j) + "\t");
            }
            System.out.println();
        }
    }

    // 打印指定9行的九九乘法表(函数重载)
    public static void print99() {
        print99(9);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章