sy05_4:使用多重循环语句

使用多重循环语句

1.请编写程序 KY5_4.java,要求程序能够输出九九乘法表。

2.程序源代码如下。

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(); 
          } 
       } 
 }

3.编译并运行KY5_4.java     

4.请将程序的运行结果写在实验报告中,并且说明程序中每一个for循环的作用。

答:运行结果:

      *  |    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循环执行System.out.print("    "+i);输出最上方右侧的列号序列;

第二个for循环执行System.out.print("-----");输出----------|-------------

第三个for循环执行System.out.print("     "+i+"    |");

              for (j=1; j<=i; j++)  { System.out.print("   "+i*j);  }   //第四个for循环

              System.out.println();输出左侧的行号序列且进行第四个for循环;

第四个for循环执行System.out.print("   "+i*j);输出i*j的值;


猜你喜欢

转载自blog.csdn.net/qq_40956679/article/details/80762783