首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Java 九九乘法表多種實作方式教學

     編輯:Java 相關知識

經典程式設計練習:九九乘法表的多種 Java 實作技巧

九九乘法表是程式設計入門的經典題目,本文展示從基礎巢狀迴圈到格式化輸出、Java 8 Stream 風格等多種實作方式,幫助初學者掌握迴圈控制、字串格式化與現代 Java API 應用。

版本一:傳統巢狀迴圈(標準教學版)


public class MultiplicationTableV1 {
    public static void main(String[] args) {
        // 列印表頭
        System.out.print("  |");
        for (int i = 1; i <= 9; i++) {
            System.out.printf("%4d", i);
        }
        System.out.println("\n--+----------------------------------------");
        
        // 列印乘法表內容
        for (int i = 1; i <= 9; i++) {
            System.out.printf("%2d|", i); // 列標題
            for (int j = 1; j <= i; j++) { // 下三角矩陣
                System.out.printf("%4d", i * j);
            }
            System.out.println();
        }
    }
}

輸出結果:


  |   1   2   3   4   5   6   7   8   9
--+----------------------------------------
 1|   1
 2|   2   4
 3|   3   6   9
 4|   4   8  12  16
 5|   5  10  15  20  25
 6|   6  12  18  24  30  36
 7|   7  14  21  28  35  42  49
 8|   8  16  24  32  40  48  56  64
 9|   9  18  27  36  45  54  63  72  81

版本二:完整矩陣對稱輸出(含上三角)


public class MultiplicationTableV2 {
    public static void main(String[] args) {
        System.out.println("完整 9x9 乘法矩陣:");
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= 9; j++) {
                System.out.printf("%4d", i * j);
            }
            System.out.println();
        }
    }
}

版本三:Java 8 Stream 函數式風格


import java.util.stream.IntStream;

public class MultiplicationTableV3 {
    public static void main(String[] args) {
        // 產生 1~9 數字流
        IntStream.rangeClosed(1, 9).forEach(i -> {
            // 每列產生乘積字串
            String row = IntStream.rangeClosed(1, i)
                .map(j -> i * j)
                .mapToObj(String::valueOf)
                .collect(Collectors.joining("\t"));
            System.out.println(i + ":\t" + row);
        });
    }
}

版本四:格式化對齊進階版(動態寬度計算)


public class MultiplicationTableV4 {
    public static void main(String[] args) {
        int max = 9;
        int maxProduct = max * max; // 81
        int colWidth = String.valueOf(maxProduct).length() + 1; // 動態計算欄寬
        
        // 表頭
        System.out.printf("%" + colWidth + "s", "");
        for (int i = 1; i <= max; i++) {
            System.out.printf("%" + colWidth + "d", i);
        }
        System.out.println();
        
        // 分隔線
        System.out.printf("%" + colWidth + "s", "");
        for (int i = 0; i < max; i++) {
            System.out.print("-".repeat(colWidth));
        }
        System.out.println();
        
        // 內容
        for (int i = 1; i <= max; i++) {
            System.out.printf("%" + colWidth + "d|", i);
            for (int j = 1; j <= i; j++) {
                System.out.printf("%" + colWidth + "d", i * j);
            }
            System.out.println();
        }
    }
}

版本五:遞迴實作(學術練習)


public class MultiplicationTableV5 {
    public static void main(String[] args) {
        printTable(1, 1);
    }
    
    static void printTable(int row, int col) {
        if (row > 9) return; // 終止條件
        
        if (col > row) {
            System.out.println();
            printTable(row + 1, 1); // 下一列
        } else {
            System.out.printf("%4d", row * col);
            printTable(row, col + 1); // 下一欄
        }
    }
}

核心觀念總結

實作方式適用場景學習重點
巢狀 for 迴圈基礎教學、面試題迴圈控制、邊界條件、輸出格式化
Stream API現代 Java 專案函數式思維、方法鏈、收集器
動態寬度計算通用表格輸出工具字串格式化、動態適應性
遞迴演算法教學遞迴終止條件、堆疊框架

延伸練習題目

  1. 參數化版本:接受命令列參數 N 列印 N×N 乘法表
  2. 對齊優化:根據最大乘積動態計算每欄寬度
  3. HTML 輸出:產生 <table> 標籤可直接嵌入網頁
  4. 多執行緒版:將每列計算分派給不同執行緒(練習 Future)
  5. GUI 版本:使用 Swing/JavaFX 製作互動式乘法表

常見錯誤與除錯技巧

  • 迴圈邊界錯誤:j <= 9 vs j <= i 決定完整矩陣 vs 下三角
  • 輸出未換行:忘記外層迴圈結束時 System.out.println()
  • 格式化字串錯誤:%4d 寬度不足導致欄位錯位
  • 變數作用域:迴圈變數在 Java 中僅在迴圈內有效
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved