zoukankan      html  css  js  c++  java
  • 循环结构进阶


                 第九章

    预备单词:

    • triangle    circle    diamond   password   row
    • 三角          循环      钻石          密码         行

    一 : 二重循环结构:

    语法:

    public class xia2 {
    
        public static void main(String[] args) {
            //while---while
            while( 条件1 ){
                
                while( 条件2 ) {
                    
                }
            }
            //do---whlie
            do{
                
                do{
                    
                }while( 条件1 );
            }while( 条件2 );
            //for与for
            for( 条件 1 ) {
                for( 条件2 ){
                    
                }
            }
            //while与while
            while(条件 1){
                
                for(条件2){
                    
                }
            }
        }
    }

    二:使用二重循环:

    import java.util.Scanner;
    
    
    public class xia2 {
    
        public static void main(String[] args) {
            int rows=0;
            System.out.print("请输入直角三角型的行数:");
            Scanner input=new Scanner(System.in);
            rows=input.nextInt();
            for (int i = 1; i <=rows; i++) {
                for (int j = 1; j <=2*i-1; j++) {
                    System.out.print("*");
                }
                System.out.println("
    ");
            }
        }
    }

     三 :跳转语句进阶

    •  continue语句:
    for(.....){
    
    for(.....){
    ......
      continue;
        .......
       }
     .......
    }

    break语句:

    for(.....){
    
    for(.....){
       ......
        break;
          ......
          }
          ......
    }
    
        
    import java.util.Scanner;
    
    
    public class xia8 {
        public static void main(String[] args) {
            int count=0;            //计数器,记录一共买了几件衣服
            String choice;
            Scanner input=new Scanner(System.in);
            for (int i = 0; i < 5; i++) {
                System.out.println("欢迎光临第"+(i+1)+"家专卖店");
                for (int j = 0; j <3; j++) {
                    System.out.println("要离开吗(y/n)");
                    choice=input.nextLine();
                    if ("y".equals(choice)) {
                        break;
                    }
                    System.out.println("买了一件衣服");
                    count++;
                }
                System.out.println("离店结账");
            }
            System.out.println("总共买了"+count+"件衣服");
            choice=input.nextLine();
        }
    }

    总结:

    二重循环就是一个循环体内又包含另一个完整的循环结构的循环

    在二重循环中可以使用break...continue语句控制程序的执行

     

                                                                                                                                                                                            2016-07-09

                                                                                                                                                                                              16:37:14

                                                                                                                                                                                             cheng.java


     

  • 相关阅读:
    二、静、动态代理(9~10)~~~~
    luogu P3572 [POI2014]PTA-Little Bird 单调队列优化dp
    luogu P6113 【模板】一般图最大匹配 带花树
    Codeforces Round #646 (Div. 2) C——Game On Leaves 思维
    Codeforces Round #646 (Div. 2) E——Tree Shuffling 思维
    luogu P2979 [USACO10JAN]Cheese Towers S 变形dp背包
    luogu P6577 【模板】二分图最大权完美匹配
    Educational Codeforces Round 88 (Rated for Div. 2) D
    Codeforces Round #645 (Div. 2) E
    Codeforces Round #645 (Div. 2) D
  • 原文地址:https://www.cnblogs.com/chengzixin/p/5656119.html
Copyright © 2011-2022 走看看