• 循环结构循环嵌套


    循环嵌套:循环语句的循环体本身是一个循环语句

    外循环控制行数

    内循环控制列数

    1 输出4行5列的星星:

    *****

    *****

    *****

    *****

    1 for(int i = 0; i < 4; i++) {
    2             for(int j = 0; j < 5; j++) {
    3                 System.out.print("*");
    4             }
    5             System.out.println();
    6         }

    2 请输出如下图形:

    *

    **

    ***

    ****

    *****

    1 for(int i = 0; i <= 4; i++) {
    2             for(int j = 0; j <= i; j++) {
    3                 System.out.print("*");
    4             }
    5             System.out.println();
    6         }

    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 * 9 = 9  2 * 9 = 18  3 * 9 = 27  4 * 9 = 36  ...  9 * 9 = 81

    为了输出九九乘法表,我们先试着输出如下图形:

    *

    **

    ***

    ****

    *****

    ******

    *******

    ********

    *********

    1 for(int i = 0; i < 9; i++) {
    2             for(int j = 0; j <= i; j++) {
    3                 System.out.print("*");
    4             }
    5             System.out.println();
    6         }

    改进:

     1 //分析:i*j
     2         //1行 : i=1,j=1, 1*1=1
     3         //2行:  i=2,j=1,2, 2*1=2,2*2=4
     4         //3行:  i=3,j=1,2,3  3*1=3,3*2=6,3*3=9
     5 
     6         for(int i = 1; i < 10; i++) {
     7             for(int j = 1; j <= i; j++) {
     8                 System.out.print(j + " * " + i + " = " + i*j + "	");
     9             }
    10             System.out.println();
    11         }

    输出结果:

    1 1 * 1 = 1    
    2 1 * 2 = 2    2 * 2 = 4    
    3 1 * 3 = 3    2 * 3 = 6    3 * 3 = 9    
    4 1 * 4 = 4    2 * 4 = 8    3 * 4 = 12    4 * 4 = 16    
    5 1 * 5 = 5    2 * 5 = 10    3 * 5 = 15    4 * 5 = 20    5 * 5 = 25    
    6 1 * 6 = 6    2 * 6 = 12    3 * 6 = 18    4 * 6 = 24    5 * 6 = 30    6 * 6 = 36    
    7 1 * 7 = 7    2 * 7 = 14    3 * 7 = 21    4 * 7 = 28    5 * 7 = 35    6 * 7 = 42    7 * 7 = 49    
    8 1 * 8 = 8    2 * 8 = 16    3 * 8 = 24    4 * 8 = 32    5 * 8 = 40    6 * 8 = 48    7 * 8 = 56    8 * 8 = 64    
    9 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

     注意:

    'x',x表示任意,这种做法叫转义字符

    ' ' 换行

    ' ' tab键

    ' ' 回车

  • 相关阅读:
    【PAT甲级】1079 Total Sales of Supply Chain (25 分)
    CQOI2018 Day1 社交网络
    codeforces 707E Garlands (离线、二维树状数组)
    NOI2018 Day1 归程(Kruskal重构树)
    NOI2018 Day2 屠龙勇士(扩展孙子定理+multiset)
    知识点:二叉(重量)平衡树——替罪羊树
    BZOJ3065 带插入区间K小值
    知识点:斜率优化DP
    知识点:FFT详解
    博客园test(搭博客用)
  • 原文地址:https://www.cnblogs.com/happy520/p/6551507.html
Copyright © 2020-2023  润新知