【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);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章