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

循环结构进阶

时间:2016-07-09 17:43:46      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:


             第九章

预备单词:

  • 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("\n");
        }
    }
}

 三 :跳转语句进阶

  •  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

 

 

 

 

 

 

 

 

 

 

 

 


 

循环结构进阶

标签:

原文地址:http://www.cnblogs.com/chengzixin/p/5656119.html

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