您当前的位置: 首页 >  Java

梁云亮

暂无认证

  • 2浏览

    0关注

    1211博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Java中的流程控制

梁云亮 发布时间:2021-09-29 09:20:48 ,浏览量:2

结构图

在这里插入图片描述

顺序结构

代码从上往下逐行执行

选择结构

语法规则:


if(条件表达式){
   //……

}else{
   //……
}
if(条件表达式){
   //……
}else if(条件表达式){
   //……
} else if(条件表达式){
   //……
}…..
else{
   //……
}

示例:

public static void main(String[] args) {
    int a =1;
    if (a>3) {
        System.out.println("大");
    }

    if (a>3) {
        System.out.println("大于");
    }else {
        System.out.println("小于");
    }

    final int score = 18;
    if (score>90) {
        System.out.println("优秀");
    }else if(score>70){
        System.out.println("良好");
    }else if(score>60){
        System.out.println("及格");
    }else {
        System.out.println("不及格");
    }
}
分支结构

语法规则:

switch(条件表达式){
	case 条件1:
      //……
break;
	case 条件2:
      //……
break;
……
default:
  //……
}

示例:

public static void main(String[] args) {
    final String jiJie = "SPRING";
    switch (jiJie){
        case "spring":
            System.out.println("春天");
            break;
        case "summer":
            System.out.println("夏天");
            break;
        case "autumn":
            System.out.println("秋天");
            break;
        case "winter":
            System.out.println("冬天");
            break;
        default:
            System.out.println("输入错误");
    }
}

示例:

public static void main(String[] args) {
    int day = 4;
    switch (day) {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            System.out.println("工作日");
            break;
        case 6:
        case 7:
            System.out.println("休息日");
            break;
        default:
            System.out.println("输入错误");
    }
}
For循环
语法:
for(数据类型 循环变量;循环继续条件;循环变量自动修正){
	//……
}

示例:

public static void main(String[] args) {
    for(int i =0;i            
关注
打赏
1665409997
查看更多评论
0.0425s