码迷,mamicode.com
首页 > 其他好文 > 详细

流程控制语句_for嵌套

时间:2014-07-16 20:57:11      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   使用   for   re   

/*
语句嵌套形式,其实就是语句中还有语句
循环嵌套


如果发现图形有很多行,没行中有很多列
要使用嵌套循环,原理就是大圈套小圈
*/
public class ForforDemo {

    public static void main(String[] args) {
        Forfor1();
    }
    
    //结果是打印长方形,外循环控制行数,内循环控制的是每一行的列数
    public static void Forfor1(){
        for (int x =0;x<3 ;x++ ){
            for (int y = 0;y<3 ;y++ ){
                System.out.print("*");
            }
            System.out.println();//只有一个功能就是换行 去掉ln一点意义都没有
        }
    }
        /*
        打印该形状
        ****
        ***
        **
        *
        */
    public static void Forfor2(){
        for (int x = 0;x<4 ;x++ ){
            //每次减少一个
            for (int y = x;y<4 ;y++ ){
                System.out.print("*");
            }
            System.out.println();
        }
    }
    
    
    /*
    打印该形状
    *
    **
    ***
    ****
    */
    public static void Forfor3(){
        for (int x = 0;x<4 ; x++){
            for (int y =0;y<=x ;y++ ){
                System.out.print("*");
            }
            System.out.println();
        }
    }
    
}
//不是规律的规律:尖朝上,可以改变条件,尖朝下,改变初始化值

 

/*
打印下面
1
12
123
1234
12345

九九乘法表
1*1=1
1*2=2 2*2=2
1*3=3 2*3=6 3*3=9
*/
public class ForforTest1 {

    public static void main(String[] args) {

    }
    
    public static void method1(){
        for (int x = 1;x<=5 ;x++ ){
            for (int y = 1;y<=x ;y++ ){
                System.out.print(y);
            }
            System.out.println();
        }
    }
    
    public static void method2(){
        int z = 9; 
        for (int x = 1;x<=z ;x++ ){
            for (int y = 1;y<=x ;y++ ){
                System.out.print(y+"*"+x+"="+y*x+"\t");//加制表符就可以对其了
            }
            System.out.println();
        }
    }

}

 

/*
----*
---* *
--* * *
-* * * *
* * * * * 
*/

public class ForforTest2 {

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

}

 

流程控制语句_for嵌套,布布扣,bubuko.com

流程控制语句_for嵌套

标签:style   blog   color   使用   for   re   

原文地址:http://www.cnblogs.com/LO-ME/p/3530564.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!