Java使用多重循环语句

九九乘法表
public class KY5_4 
     { 
       public static void main(String args[]) 
       { 
          int i, j, n=9; 
          System.out.print("      *   |"); 
          for (i=1; i<=n; i++)  { System.out.print("    "+i);  }   //第一个for循环
          System.out.print("\n----------|"); 
          for (i=1; i<=n; i++)  { System.out.print("-----");  }     //第二个for循环 
          System.out.println(); 
          for (i=1; i<=n; i++)                               //第三个for循环
          { 
              System.out.print("     "+i+"    |"); 
              for (j=1; j<=i; j++)  { System.out.print("   "+i*j);  }   //第四个for循环 
              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
第一个for循环:输出了  *   |    1    2    3    4    5    6    7    8    9
第二个for循环:输出了----------|---------------------------------------------
第三个for循环:输出左边的一列1到9的列
第四个for循环:输出九九数乘法表的各个结果,即:
1
2   4
3   6   9
4   8   12   16
5   10   15   20   25
6   12   18   24   30   36
7   14   21   28   35   42   49
8   16   24   32   40   48   56   64
9   18   27   36   45   54   63   72   81


猜你喜欢

转载自blog.csdn.net/gayhang/article/details/80787781