• 循环结构


    break和continue
    break:终止,结束(表示终止当前循环结构)

     1 package chapter9;
     2 
     3 import java.util.Scanner;
     4 
     5 public class class03 {
     6     public static void main(String[] args){
     7         Scanner input=new Scanner(System.in);
     8         int count=0;
     9         for(int i=0;i<5;i++){
    10             System.out.println("欢迎光临第"+(i+1)+"家店");
    11             for(int j=0;j<3;j++){
    12                 System.out.print("要离开吗(y/n)?");
    13                 String str=input.next();
    14                 if(str.equals("y")){
    15                     break;
    16                 }
    17                 count++;
    18                 System.out.println("买了一件衣服");
    19             }
    20             System.out.println("离店结账
    ");
    21         }
    22         System.out.println("总共买了:"+count+"件");
    23     }
    24 }
    示例代码


    continue:继续(表示结束本轮循环,进入下一轮循环)

     1 package chapter9;
     2 
     3 import java.util.Scanner;
     4 
     5 public class class02 {
     6     public static void main(String[] args){
     7         Scanner input=new Scanner(System.in);
     8         double avg=0;
     9         int count=0;
    10         for(int k=0;k<3;k++){
    11             double sum=0;
    12             System.out.println("第"+(k+1)+"个班级的成绩");
    13             for(int i=0;i<4;i++){
    14                 System.out.print("请输入第"+(i+1)+"个成绩:");
    15                 int score=input.nextInt();
    16                 sum=sum+score;
    17                 if(score<85){
    18                     continue;
    19                 }
    20                 count++;
    21             }
    22             avg=sum/4;
    23             System.out.println("平均分:"+avg+"
    ");
    24         }
    25         System.out.println("成绩85分以上的学员人数有"+count+"人");
    26     }
    27 }
    代码示例


    注意:多层循环,只会对直接的循环起作用


    二重循环
    语法:
    while与while循环嵌套
    do-while与do-while循环嵌套
    for与for循环嵌套
    while与for循环嵌套

  • 相关阅读:
    【转】Android——设置颜色的三种方法
    Eclipse Android安装APP时覆盖安装问题
    自定义数组,实现输出改数组的长度、最大值和最小值
    用程序实现对数组a[45,96,78,6,18,66,50]中的元素进行排序
    PHP面试题2
    PHP面试题
    gulp
    移动端base.css
    笔记
    mouseover和mouseout事件在鼠标经过子元素时也会触发
  • 原文地址:https://www.cnblogs.com/yang82/p/6920521.html
Copyright © 2020-2023  润新知