zoukankan      html  css  js  c++  java
  • 嵌套循环概念


    嵌套循环:循环体中还可以声明循环,相当于内层循环的整体充当外层循环的循环体
    例:
    for(;;){
    for(;;){

    }
    }
    或者
    while(){
    for(;;){

    }
    }
    题目:输出四排*,要求用嵌套循环
    *
    **
    ***
    ****

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



    ****
    ***
    **
    *

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



    *
    **
    ***
    ****
    ***
    **
    *

    public class V{
        public static void main(String[] args){
    
            for (int i=0;i<4;i++){//上半部分
                for (int j=0;j<i+1;j++) {
                    System.out.print("*");
                }
                System.out.println();
            }
            for (int i=0;i<3;i++){//下半部分
                for (int z=0;z<3-i;z++){
                    System.out.print("*");
                }
                System.out.println();
        }
       }
    }


        *

       * *

      * * *

     * * * * 

    * * * * *

     * * * *

      * * *

       * * 

        *

    public class V{
        public static void main(String[] args){
            for (int i=0;i<5;i++){//上半部分
               for (int j=0;j<4-i;j++){
                   System.out.print(" ");
               }
               for (int z=0;z<i+1;z++){
                   System.out.print("* ");
               }
               System.out.println();
           }
            for (int i=0;i<4;i++){//下半部分
               for (int j=0;j<i+1;j++){
                   System.out.print(" ");
               }
               for (int z=0;z<4-i;z++){
                   System.out.print("* ");
               }
               System.out.println();
           }
        }
    }



    题目:九九乘法表

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


    题目:1-100之间所有质数

    public class V{
        public static void main(String[] args){
            boolean flag=false;
            for (int i=2;i<=100;i++){
                for (int j=2;j<i;j++){
                    if (i%j==0){
                        flag=true;
                    }
                }
                if (flag==false){
                    System.out.println(i);
                }
                flag=false;
            }
        }
    }

  • 相关阅读:
    Python文件操作
    两个标签页定位第二个标签页元素时显示element not visible
    Selenium Webdriver元素定位的八种常用方式
    Python集合set
    Python 字典
    数据库存储过程
    打印1-100
    》》》 《类的多态》
    》》》《类的继承》
    》》》关于eclipse中git同步代码报错checkout conflict with files的解决方法
  • 原文地址:https://www.cnblogs.com/KeepCalmAndNeverSayNever/p/10074317.html
Copyright © 2011-2022 走看看