Zeruns's Blog

Java实现输出九九乘法表—for循环和递归算法

Java实现输出99乘法表

for循环


public class ninenine {
    public static void main(String[] args){
        for (int i=1;i<10;i++) {
            for(int j=1;j<=i;j++) {
                System.out.printf("%d*%d=%d\t",j,i,j*i);
            }
            System.out.println("");
        }
    }
}

递归

public class MultiTable {
 public static void main(String args[]) { 
    m(9); 
  } 
  
  /** 
   * 打印出九九乘法表 
   * https://blog.zeruns.tech
   */
  public static void m(int i) { 
    if (i == 1) { 
      System.out.println("1*1=1 "); 
    } else { 
      m(i - 1); 
      for (int j = 1; j <= i; j++) { 
        System.out.print(j + "*" + i + "=" + j * i + " "); 
      } 
      System.out.println(); 
    } 
  }  
}

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »