day-07

流程控制

顺序结构:java的基本结构就是顺序结构,除非特别指明,否则按照顺序一步一步的执行
顺序结构是最简单的算法结构

public class ShunXuDem0 {
    public static void main(String[] args) {
        System.out.println("hello1");
        System.out.println("hello2");
        System.out.println("hello3");
        System.out.println("hello4");
        System.out.println("hello5");
    }
}

选择结构
if单选择结构

public class IfDemo01 {
    public static void main(String[] args) {
        Scanner scanner =new Scanner(System.in);
        System.out.println("请输入数据");
        String s =scanner.nextLine();
        if(s.equals("hello")){
            System.out.println(s);
        }
        System.out.println("end");
        scanner.close();
    }
}

if双选择结构

public class IfDemo02 {
    public static void main(String[] args) {
        //考试分数大于60分就是合格,小于60分就是不合格
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入数据");
        int score = scanner.nextInt();
        if (score > 60) {
            System.out.println("及格");
        } else {
            System.out.println("不及格");
        }
        scanner.close();
    }
}

if多选择结构

public class IfDemo03 {
    public static void main(String[] args) {
        //考试分数大于60分就是合格,小于60分就是不合格
        Scanner scanner = new Scanner(System.in);
        /*if语句至少有一个else语句,else语句在所有的else if语句之后,
        一旦其中一个else if语句为true,则其他的else if语句以及else语句都将跳过执行

       */
        System.out.println("请输入数据");

        int score = scanner.nextInt();
        if (score ==100) {
            System.out.println("恭喜满分");
        } else if (score<100&&score>90){
            System.out.println("A级");
        }else if (score<90&&score>80){
            System.out.println("B级");
        }else if (score<80&&score>70){
            System.out.println("C级");
        }else if (score<70&&score>60){
            System.out.println("D级");
        }else if (score<60&&score>=0){
            System.out.println("不及格");
        }else{
            System.out.println("成绩不合法");
        }
        scanner.close();
    }
}

嵌套的if结构
就是if里面还可以嵌套if罢了

switch多选择结构
switch case语句判断一个变量和一系列值中某个是否相等,每个值成为一个分支
switch语句中的变量类型可以是:byte,short,int,或者char
也支持字符串String类型,同事case标签可以是字符串常量或者字面量

case穿透现象,switch匹配一个具体的值

public class SwitchDemo01 {
    public static void main(String[] args) {
        char grade ='C';
        switch (grade){
            case'A':
                System.out.println("优秀");
                break;//可选
            case 'B':
                System.out.println("良好");
                break;//可选
            case 'C':
                System.out.println("及格");
                break;//可选
            case 'D':
                System.out.println("再接再厉");
                break;//可选
            case 'E':
                System.out.println("挂科");
                break;//可选
            default:
                System.out.println("未知等级");
        }
    }
}
public class SwitchDemo02 {
    public static void main(String[] args) {
        String name="程乐";
        switch (name){
            case "程乐":
                System.out.println("程乐");
                break;
            case "程小乐":
                System.out.println("程小乐");
            default:
                System.out.println("弄啥嘞");
        }

    }
}

循环结构
while,do whlie,for循环
whlie为最基本的循环
只要布尔值为true,程序就会一直走下去

public class WhlieDemo01 {
    public static void main(String[] args) {
        //输出1~100
        int i=0;
        while (i<100){
            i++;
            System.out.println(i);
        }
    }
}

大多数情况下要让循环停下来,我们需要一个让表达式失效的方式来结束循环
少数情况下需要一直执行下去,比如服务器的请求响应监听
循环一直为true就会造成无限循环

public class WhlieDemo02 {
    public static void main(String[] args) {
        while (true){
            //等待客户端连接
            //定时检查
        }
    }
}
public class WhlieDemo03 {
    public static void main(String[] args) {
        //计算1+2+3+...+100?
        int i=0;
        int sum =0;
        while (i<=100){
            sum =sum+i;
            i++;
        }
        System.out.println(sum);
    }
}

对于while,如果不能满足条件,则不能进入循环
do while和while循环类似,不同的是,do while至少循环一次
while是先判断后执行,do while是先执行后判断

public class DoWhileDemo01 {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;
        do {
            sum = sum + i;
            i++;
        } while (i<=100);
        System.out.println(sum);
    }
}

比较区别

public class DoWhileDemo02 {
    public static void main(String[] args) {
        int a=0;
        while (a<0){
            System.out.println(a);
            a++;
        }
        System.out.println("===============");
        do {
            System.out.println(a);
            a++;
        }while (a<0);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值