基础语法-for循环的嵌套

                                       作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

 

一.for循环嵌套概述

  说白了就是在for循环中再嵌套一层for循环。

 

二.for循环嵌套案例

1>.打印直角三角形

/**
 *     for循环嵌套
 * @author 尹正杰
 *
 */
public class ForDemo02 {

    public static void main(String[] args) {
        
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

}

基础语法-for循环的嵌套

2>.倒着打印直角三角形

/**
 *     for循环嵌套
 * @author 尹正杰
 *
 */
public class ForDemo02 {

    public static void main(String[] args) {
        
        for (int i = 0; i < 5; i++) {
            for (int j = i; j < 5; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

}

基础语法-for循环的嵌套

3>.倒着打印数字

/**
 *     for循环嵌套
 * @author 尹正杰
 *
 */
public class ForDemo02 {

    public static void main(String[] args) {
        
        for (int i = 1; i <= 5; i++) {
            for (int j = 5; j >= i; j--) {
                System.out.print(j);
            }
            System.out.println();
        }
    }

}

基础语法-for循环的嵌套

4>.打印99乘法表

/**
 *     for循环嵌套
 * @author 尹正杰
 *
 */
public class ForDemo02 {

    public static void main(String[] args) {
        
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + "x" + i + "=" + (j * i) + "\t");
            }
            System.out.println();
        }
    }

}

基础语法-for循环的嵌套

 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-10
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-30
相关资源
相似解决方案