ETJava Beta | Java    注册   登录
  • java多种方式实现打印九九乘法表

    发表于 2025-12-02 08:29:10     阅读(35)     博客类别:J2SE

    1 标准九九乘法表

     

    package org.test;
    
    public class MultiplicationTable1 {
        public static void main(String[] args) {
            System.out.println("=== 标准九九乘法表 ===");
            
            for (int i = 1; i <= 9; i++) {          // 外层循环控制行
                for (int j = 1; j <= i; j++) {      // 内层循环控制列,只打印到i
                    System.out.printf("%d×%d=%2d  ", j, i, i * j);
                }
                System.out.println();  // 每行结束换行
            }
        }
    }
    
    // 执行结果
    === 标准九九乘法表 ===
    1×1=1   
    1×2=2   2×2=4   
    1×3=3   2×3=6   3×3=9   
    1×4=4   2×4=8   3×4=12  4×4=16  
    1×5=5   2×5=10  3×5=15  4×5=20  5×5=25  
    1×6=6   2×6=12  3×6=18  4×6=24  5×6=30  6×6=36  
    1×7=7   2×7=14  3×7=21  4×7=28  5×7=35  6×7=42  7×7=49  
    1×8=8   2×8=16  3×8=24  4×8=32  5×8=40  6×8=48  7×8=56  8×8=64  
    1×9=9   2×9=18  3×9=27  4×9=36  5×9=45  6×9=54  7×9=63  8×9=72  9×9=81  

     

    2 while循环实现

     

    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.println();
                i++;
            }
    }
    
    
    === while循环实现 ===
    1×1=1	
    1×2=2	2×2=4	
    1×3=3	2×3=6	3×3=9	
    1×4=4	2×4=8	3×4=12	4×4=16	
    1×5=5	2×5=10	3×5=15	4×5=20	5×5=25	
    1×6=6	2×6=12	3×6=18	4×6=24	5×6=30	6×6=36	
    1×7=7	2×7=14	3×7=21	4×7=28	5×7=35	6×7=42	7×7=49	
    1×8=8	2×8=16	3×8=24	4×8=32	5×8=40	6×8=48	7×8=56	8×8=64	
    1×9=9	2×9=18	3×9=27	4×9=36	5×9=45	6×9=54	7×9=63	8×9=72	9×9=81	

     

    3 do-while循环实现

     

    public static void main(String[] args) {
            System.out.println("=== do-while循环实现 ===");
            
            int i = 1;
            do {
                int j = 1;
                do {
                    System.out.printf("%d×%d=%-2d  ", j, i, i * j);
                    j++;
                } while (j <= i);
                System.out.println();
                i++;
            } while (i <= 9);
    }
    
    === do-while循环实现 ===
    1×1=1   
    1×2=2   2×2=4   
    1×3=3   2×3=6   3×3=9   
    1×4=4   2×4=8   3×4=12  4×4=16  
    1×5=5   2×5=10  3×5=15  4×5=20  5×5=25  
    1×6=6   2×6=12  3×6=18  4×6=24  5×6=30  6×6=36  
    1×7=7   2×7=14  3×7=21  4×7=28  5×7=35  6×7=42  7×7=49  
    1×8=8   2×8=16  3×8=24  4×8=32  5×8=40  6×8=48  7×8=56  8×8=64  
    1×9=9   2×9=18  3×9=27  4×9=36  5×9=45  6×9=54  7×9=63  8×9=72  9×9=81  

     

     

    4 for循环带表格

     

    public static void main(String[] args) {
            System.out.println("╔════════════════════════════════════════════════════════════╗");
            System.out.println("║                   九九乘法表                       ║");
            System.out.println("╠════════════════════════════════════════════════════════════╣");
            
            for (int i = 1; i <= 9; i++) {
                System.out.print("║ ");
                for (int j = 1; j <= i; j++) {
                    // 使用格式化输出确保对齐
                    System.out.printf("%d×%d=%-2d", j, i, i * j);
                    if (j < i) {
                        System.out.print(" | ");
                    }
                }
                // 填充空格使右边对齐
                int spaces = (9 - i) * 6;
                for (int k = 0; k < spaces; k++) {
                    System.out.print(" ");
                }
                System.out.println(" ║");
            }
            
            System.out.println("╚═══════════════════════════════════════════════════════════╝");
    }
    
    
    ╔════════════════════════════════════════════════════════════╗
    ║                   九九乘法表                       ║
    ╠════════════════════════════════════════════════════════════╣
    ║ 1×1=1                                                  ║
    ║ 1×2=2  | 2×2=4                                            ║
    ║ 1×3=3  | 2×3=6  | 3×3=9                                      ║
    ║ 1×4=4  | 2×4=8  | 3×4=12 | 4×4=16                               ║
    ║ 1×5=5  | 2×5=10 | 3×5=15 | 4×5=20 | 5×5=25                         ║
    ║ 1×6=6  | 2×6=12 | 3×6=18 | 4×6=24 | 5×6=30 | 6×6=36                   ║
    ║ 1×7=7  | 2×7=14 | 3×7=21 | 4×7=28 | 5×7=35 | 6×7=42 | 7×7=49             ║
    ║ 1×8=8  | 2×8=16 | 3×8=24 | 4×8=32 | 5×8=40 | 6×8=48 | 7×8=56 | 8×8=64       ║
    ║ 1×9=9  | 2×9=18 | 3×9=27 | 4×9=36 | 5×9=45 | 6×9=54 | 7×9=63 | 8×9=72 | 9×9=81 ║
    ╚═══════════════════════════════════════════════════════════╝
    
    

     

    5 递归实现九九乘法表

     

    public static void main(String[] args) {
            System.out.println("=== 递归实现九九乘法表 ===");
            printRow(1);
    }
        
        // 递归打印行
        private static void printRow(int i) {
            if (i > 9) return;
            
            printColumn(i, 1);
            System.out.println();
            
            printRow(i + 1);
        }
        
        // 递归打印列
        private static void printColumn(int i, int j) {
            if (j > i) return;
            
            System.out.printf("%d×%d=%-2d  ", j, i, i * j);
            printColumn(i, j + 1);
        }
    
    
    
    
    
    === 递归实现九九乘法表 ===
    1×1=1   
    1×2=2   2×2=4   
    1×3=3   2×3=6   3×3=9   
    1×4=4   2×4=8   3×4=12  4×4=16  
    1×5=5   2×5=10  3×5=15  4×5=20  5×5=25  
    1×6=6   2×6=12  3×6=18  4×6=24  5×6=30  6×6=36  
    1×7=7   2×7=14  3×7=21  4×7=28  5×7=35  6×7=42  7×7=49  
    1×8=8   2×8=16  3×8=24  4×8=32  5×8=40  6×8=48  7×8=56  8×8=64  
    1×9=9   2×9=18  3×9=27  4×9=36  5×9=45  6×9=54  7×9=63  8×9=72  9×9=81 

     

     

    6 逆序九九乘法表

     

    public static void main(String[] args) {
            System.out.println("=== 逆序九九乘法表 ===");
            
            for (int i = 9; i >= 1; i--) {
                for (int j = 1; j <= i; j++) {
                    System.out.printf("%d×%d=%-2d  ", j, i, i * j);
                }
                System.out.println();
            }
        }
    
    
    === 逆序九九乘法表 ===
    1×9=9   2×9=18  3×9=27  4×9=36  5×9=45  6×9=54  7×9=63  8×9=72  9×9=81  
    1×8=8   2×8=16  3×8=24  4×8=32  5×8=40  6×8=48  7×8=56  8×8=64  
    1×7=7   2×7=14  3×7=21  4×7=28  5×7=35  6×7=42  7×7=49  
    1×6=6   2×6=12  3×6=18  4×6=24  5×6=30  6×6=36  
    1×5=5   2×5=10  3×5=15  4×5=20  5×5=25  
    1×4=4   2×4=8   3×4=12  4×4=16  
    1×3=3   2×3=6   3×3=9   
    1×2=2   2×2=4   
    1×1=1  

     

     

    7 使用StringBuilder优化

     

    public static void main(String[] args) {
            System.out.println("=== 使用StringBuilder ===");
            
            StringBuilder table = new StringBuilder();
            
            for (int i = 1; i <= 9; i++) {
                for (int j = 1; j <= i; j++) {
                    table.append(j).append("×").append(i).append("=").append(i * j);
                    if (j < i) {
                        table.append("\t");
                    }
                }
                table.append("\n");
            }
            
            System.out.print(table.toString());
        }
    
    
    === 使用StringBuilder ===
    1×1=1
    1×2=2	2×2=4
    1×3=3	2×3=6	3×3=9
    1×4=4	2×4=8	3×4=12	4×4=16
    1×5=5	2×5=10	3×5=15	4×5=20	5×5=25
    1×6=6	2×6=12	3×6=18	4×6=24	5×6=30	6×6=36
    1×7=7	2×7=14	3×7=21	4×7=28	5×7=35	6×7=42	7×7=49
    1×8=8	2×8=16	3×8=24	4×8=32	5×8=40	6×8=48	7×8=56	8×8=64
    1×9=9	2×9=18	3×9=27	4×9=36	5×9=45	6×9=54	7×9=63	8×9=72	9×9=81
    
    

     

     

    8 左右,居中

     

    public class Test{
        public static void main(String[] args) {
            System.out.println("=== 对齐美观版 ===");
            System.out.println();
            
            System.out.println("1. 左对齐版:");
            printLeftAligned();
            
            System.out.println("\n2. 右对齐版:");
            printRightAligned();
            
            System.out.println("\n3. 居中对齐版:");
            printCenterAligned();
            
            System.out.println("\n4. 紧凑版:");
            printCompact();
        }
        
        // 左对齐
        private static void printLeftAligned() {
            for (int i = 1; i <= 9; i++) {
                for (int j = 1; j <= i; j++) {
                    System.out.printf("%d×%d=%-2d  ", j, i, i * j);
                }
                System.out.println();
            }
        }
        
        // 右对齐
        private static void printRightAligned() {
            for (int i = 1; i <= 9; i++) {
                // 打印前导空格
                for (int k = 1; k < i; k++) {
                    System.out.print("        "); // 8个空格
                }
                
                for (int j = i; j <= 9; j++) {
                    System.out.printf("%d×%d=%-2d  ", i, j, i * j);
                }
                System.out.println();
            }
        }
        
        // 居中对齐(三角形)
        private static void printCenterAligned() {
            for (int i = 1; i <= 9; i++) {
                // 居中前导空格
                int leadingSpaces = (9 - i) * 4;
                for (int s = 0; s < leadingSpaces; s++) {
                    System.out.print(" ");
                }
                
                for (int j = 1; j <= i; j++) {
                    System.out.printf("%d×%d=%-2d  ", j, i, i * j);
                }
                System.out.println();
            }
        }
        
        // 紧凑版
        private static void printCompact() {
            for (int i = 1; i <= 9; i++) {
                for (int j = 1; j <= i; j++) {
                    // 使用条件运算符减少空格
                    String separator = (i * j < 10) ? "  " : " ";
                    System.out.print(j + "×" + i + "=" + (i * j) + separator);
                }
                System.out.println();
            }
        }
    }
    
    === 对齐美观版 ===
    
    1. 左对齐版:
    1×1=1   
    1×2=2   2×2=4   
    1×3=3   2×3=6   3×3=9   
    1×4=4   2×4=8   3×4=12  4×4=16  
    1×5=5   2×5=10  3×5=15  4×5=20  5×5=25  
    1×6=6   2×6=12  3×6=18  4×6=24  5×6=30  6×6=36  
    1×7=7   2×7=14  3×7=21  4×7=28  5×7=35  6×7=42  7×7=49  
    1×8=8   2×8=16  3×8=24  4×8=32  5×8=40  6×8=48  7×8=56  8×8=64  
    1×9=9   2×9=18  3×9=27  4×9=36  5×9=45  6×9=54  7×9=63  8×9=72  9×9=81  
    
    2. 右对齐版:
    1×1=1   1×2=2   1×3=3   1×4=4   1×5=5   1×6=6   1×7=7   1×8=8   1×9=9   
            2×2=4   2×3=6   2×4=8   2×5=10  2×6=12  2×7=14  2×8=16  2×9=18  
                    3×3=9   3×4=12  3×5=15  3×6=18  3×7=21  3×8=24  3×9=27  
                            4×4=16  4×5=20  4×6=24  4×7=28  4×8=32  4×9=36  
                                    5×5=25  5×6=30  5×7=35  5×8=40  5×9=45  
                                            6×6=36  6×7=42  6×8=48  6×9=54  
                                                    7×7=49  7×8=56  7×9=63  
                                                            8×8=64  8×9=72  
                                                                    9×9=81  
    
    3. 居中对齐版:
                                    1×1=1   
                                1×2=2   2×2=4   
                            1×3=3   2×3=6   3×3=9   
                        1×4=4   2×4=8   3×4=12  4×4=16  
                    1×5=5   2×5=10  3×5=15  4×5=20  5×5=25  
                1×6=6   2×6=12  3×6=18  4×6=24  5×6=30  6×6=36  
            1×7=7   2×7=14  3×7=21  4×7=28  5×7=35  6×7=42  7×7=49  
        1×8=8   2×8=16  3×8=24  4×8=32  5×8=40  6×8=48  7×8=56  8×8=64  
    1×9=9   2×9=18  3×9=27  4×9=36  5×9=45  6×9=54  7×9=63  8×9=72  9×9=81  
    
    4. 紧凑版:
    1×1=1  
    1×2=2  2×2=4  
    1×3=3  2×3=6  3×3=9  
    1×4=4  2×4=8  3×4=12 4×4=16 
    1×5=5  2×5=10 3×5=15 4×5=20 5×5=25 
    1×6=6  2×6=12 3×6=18 4×6=24 5×6=30 6×6=36 
    1×7=7  2×7=14 3×7=21 4×7=28 5×7=35 6×7=42 7×7=49 
    1×8=8  2×8=16 3×8=24 4×8=32 5×8=40 6×8=48 7×8=56 8×8=64 
    1×9=9  2×9=18 3×9=27 4×9=36 5×9=45 6×9=54 7×9=63 8×9=72 9×9=81 
    
    
    
    
    

     

     

    9 彩色输出

     

    public class MultiplicationTable9 {
        // ANSI颜色代码
        public static final String RESET = "\033[0m";
        public static final String RED = "\033[31m";
        public static final String GREEN = "\033[32m";
        public static final String YELLOW = "\033[33m";
        public static final String BLUE = "\033[34m";
        public static final String PURPLE = "\033[35m";
        public static final String CYAN = "\033[36m";
        
        public static void main(String[] args) {
            System.out.println("=== 彩色九九乘法表 ===");
            
            String[] colors = {RED, GREEN, YELLOW, BLUE, PURPLE, CYAN};
            
            for (int i = 1; i <= 9; i++) {
                String color = colors[(i - 1) % colors.length];
                
                for (int j = 1; j <= i; j++) {
                    String result = String.format("%d×%d=%-2d", j, i, i * j);
                    
                    // 根据结果大小使用不同颜色
                    if (i * j < 10) {
                        System.out.print(color + result + RESET + "    ");
                    } else if (i * j < 20) {
                        System.out.print(color.toUpperCase().replace("[", "[1;") + result + RESET + "    ");
                    } else {
                        System.out.print(color + result + RESET + "    ");
                    }
                }
                System.out.println();
            }
        }
    }

     

     

    10 Lambda表达式方式

     

    public static void main(String[] args) {
            System.out.println("=== 使用Java 8 Stream API ===");
            
            IntStream.rangeClosed(1, 9)
                    .forEach(i -> {
                        IntStream.rangeClosed(1, i)
                                .forEach(j -> System.out.printf("%d×%d=%-2d  ", j, i, i * j));
                        System.out.println();
                    });
    }
    
    
    === 使用Java 8 Stream API ===
    1×1=1   
    1×2=2   2×2=4   
    1×3=3   2×3=6   3×3=9   
    1×4=4   2×4=8   3×4=12  4×4=16  
    1×5=5   2×5=10  3×5=15  4×5=20  5×5=25  
    1×6=6   2×6=12  3×6=18  4×6=24  5×6=30  6×6=36  
    1×7=7   2×7=14  3×7=21  4×7=28  5×7=35  6×7=42  7×7=49  
    1×8=8   2×8=16  3×8=24  4×8=32  5×8=40  6×8=48  7×8=56  8×8=64  
    1×9=9   2×9=18  3×9=27  4×9=36  5×9=45  6×9=54  7×9=63  8×9=72  9×9=81  
    
    
    

     

    11 多种选择

     

    public class MultiplicationTable10 {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            
            while (true) {
                System.out.println("\n========== 九九乘法表生成器 ==========");
                System.out.println("1. 标准版");
                System.out.println("2. 逆序版");
                System.out.println("3. 右对齐版");
                System.out.println("4. 带边框版");
                System.out.println("5. 退出");
                System.out.print("请选择(1-5): ");
                
                int choice = scanner.nextInt();
                
                switch (choice) {
                    case 1:
                        printStandard();
                        break;
                    case 2:
                        printReverse();
                        break;
                    case 3:
                        printRightAligned();
                        break;
                    case 4:
                        printWithBorder();
                        break;
                    case 5:
                        System.out.println("谢谢使用!");
                        scanner.close();
                        return;
                    default:
                        System.out.println("输入错误,请重新选择!");
                }
            }
        }
        
        private static void printStandard() {
            System.out.println("\n标准九九乘法表:");
            for (int i = 1; i <= 9; i++) {
                for (int j = 1; j <= i; j++) {
                    System.out.printf("%d×%d=%-2d  ", j, i, i * j);
                }
                System.out.println();
            }
        }
        
        private static void printReverse() {
            System.out.println("\n逆序九九乘法表:");
            for (int i = 9; i >= 1; i--) {
                for (int j = 1; j <= i; j++) {
                    System.out.printf("%d×%d=%-2d  ", j, i, i * j);
                }
                System.out.println();
            }
        }
        
        private static void printRightAligned() {
            System.out.println("\n右对齐九九乘法表:");
            for (int i = 1; i <= 9; i++) {
                for (int k = 1; k < i; k++) {
                    System.out.print("        ");
                }
                for (int j = i; j <= 9; j++) {
                    System.out.printf("%d×%d=%-2d  ", i, j, i * j);
                }
                System.out.println();
            }
        }
        
        private static void printWithBorder() {
            System.out.println("\n┌─────────────────────────────────────────┐");
            System.out.println("│          九九乘法表(带边框)          │");
            System.out.println("├─────────────────────────────────────────┤");
            
            for (int i = 1; i <= 9; i++) {
                System.out.print("│ ");
                for (int j = 1; j <= i; j++) {
                    System.out.printf("%d×%d=%-2d", j, i, i * j);
                    if (j < i) System.out.print(" ");
                }
                
                // 填充空格
                for (int k = i; k < 9; k++) {
                    System.out.print("      ");
                }
                System.out.println(" │");
            }
            
            System.out.println("└─────────────────────────────────────────┘");
        }
    }