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

  • 相关阅读:
    touch:创建文件及修改文件时间戳
    stat:查看文件时间参数
    获取二维数组里面实际存有数据的行数
    Math.Atan2 方法
    c#移位运算符("<<"及">>")
    c# 一维数组和二维数组的几种定义方式<转>
    C#异常处理总结
    C#图片灰度处理(位深度24→位深度8)、C#图片二值化处理(位深度8→位深度1)
    WinForm窗体及其控件的自适应
    C#的WinForm窗体美化
  • 原文地址:https://www.cnblogs.com/KeepCalmAndNeverSayNever/p/10074317.html
Copyright © 2011-2022 走看看