java控制语句的使用

来源:互联网 发布:软件有什么特点 编辑:程序博客网 时间:2024/04/30 19:16

if语句的使用

流程图

这里写图片描述

    int score;    while(true){        Scanner sca=new Scanner(System.in);        score=sca.nextInt();        if (score > 90) {            System.out.println("你的成绩优秀");        } else if (score > 80) {            System.out.println("你的成绩良好");        } else if (score > 70) {            System.out.println("你的成绩中等");        } else if (score >= 60) {            System.out.println("你的成绩及格");        } else {            System.out.println("你的成绩不及格");        }    }

if()括号里边需要附上一个boolean值
这里写图片描述

switch语句的使用

流程图

这里写图片描述

    int score;    while(true){        Scanner sca=new Scanner(System.in);        score=sca.nextInt();        switch (score/10){        case 10:            System.out.println("你的成绩满分");            break;        case 9:            System.out.println("你的成绩优秀");            break;        case 8:            System.out.println("你的成绩良好");            break;        case 7:            System.out.println("你的成绩中等");            break;        case 6:            System.out.println("你的成绩及格");            break;        default:            System.out.println("你的成绩不及格");            break;        }    }

switch()括号里是一个int型的值,然后下边case的值与switch的值进行比较,如果相等,则执行case下边的语句,然后break退出,否则继续和下一个case进行比较匹配。
这里写图片描述

三种循环语句的使用

for循环

流程图

这里写图片描述

        int sum=0;        for(int i=1;i<1001;i++){            sum+=i;                 }        System.out.println(sum);

while循环

流程图

这里写图片描述

        int i=1;        int sum=0;        while(i<1001){            sum+=i;            i++;        }        System.out.println(sum);

do……while循环

流程图

这里写图片描述

        int i=1;        int sum=0;        do{            sum+=i;            i++;        }        while(i<1001);        System.out.println(sum);

while 循环跟do……while循环的区别在于后者先运行code后判断是否循环,至少有一次输出结果,前者先判断是否要运行,不符合条件则直接退出,可能没有一次运行得到结果

补充

continue和break
continue:跳出本次循环,接着从循环体开始的地方开始下一次循环。
break:跳出循环体,表示循环已经全部结束。

0 0
原创粉丝点击