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


     

  • 相关阅读:
    P1135 奇怪的电梯题解
    P1443 马的遍历题解
    P2392 kkksc03考前临时抱佛脚题解
    P1219 八皇后问题题解
    IDEA导入/导出live templates或者其他设置
    P3743 kotori的设备题解
    带你看论文丨全局信息对于图网络文档解析的影响
    【“互联网+”大赛华为云赛道】EI命题攻略:华为云EI的能力超丰富,助你实现AI梦想
    【“互联网+”大赛华为云赛道】IoT命题攻略:仅需四步,轻松实现场景智能化设计
    只需6步,教你从零开发一个签到小程序
  • 原文地址:https://www.cnblogs.com/chengzixin/p/5656119.html
Copyright © 2011-2022 走看看