程序执行流程语句

2020-08-12  本文已影响0人  NingSpeals

if-else语法

public class BuyBaoZi {
    public static void main(String[] args){
        int baozi = 3;
        boolean iSHotBaozi = false;
        System.out.println("买了"+baozi+"个肉包子");
        if (iSHotBaozi){
            baozi = baozi + 2;
            System.out.println("包子刚出笼,买了"+baozi+"个肉包子");
        }else{
            System.out.println("买了"+baozi+"个肉包子");
        }
    }
}

if-else的嵌套

求最大的数

public class JudgeSize {
    public static void main(String[] args) {
        int a = 99;
        int b = 88;
        int c = 99;
        if (a == b && b == c) {
            System.out.println("a,b,c的值相同");
        } else {
            if (a > b) {
                if (a > c) {
                    System.out.println("a是最大的值,为" + a);
                } else {
                    if (a == c) {
                        System.out.println("a和c的最大值,为" + a);
                    } else {
                        System.out.println("c是最大值,为" + c);
                    }
                }
            } else {
                if (b > c) {
                    if (a == b) {
                        System.out.println("a和b等大,为" + a);
                    } else {
                        System.out.println("b最大,为" + b);
                    }
                } else {
                    if (c == b) {
                        System.out.println("b和c等大,为" + c);
                    } else {
                        System.out.println("c最大,为" + c);
                    }
                }
            }
        }
    }
}

循环for语句

for (初始语句;循环体条件表达式;循环体后语句) {
     for循环
}

下面来看下for语句的简单例程:

public class Print26Chars {
    public static  void main(String[] args){
        char ch = 'A';
        int num = ch;
        for (int i = 0;i<26;i++){
            System.out.println((num+i) + "\t"+(char)(num+i));
        }
    }
}

Break语句

public class FindDiv {
    public static void main(String[] args) {
        int div = 100;
        int divsor = 2000000000;
        int found = 0;
        int n = 10;
        String start = "从" + div + "开始";
        while (found < n){
            if (div < 0){
                System.out.println("被除数溢出,计算结束");
            break;
            }
            if (div%divsor == 0){
                found++;
                System.out.println(div + "可以整除" + divsor + ",商是" + (div / divsor));
            }
            div++;
        }
        System.out.println(start+",共找到"+found+"个可以整除"+divsor+"的数。");
        System.out.println(div);
    }
}

continue语句

while语句

while(条件表达式){
       while循环体;
}
do {
      while循环体;
}
while(条件表达式);

switch语句

switch(用于比较的int值){
    case 目标值1,对应一个 if else(xxx):
           匹配后可以执行的语句 
    case 目标值2,不可以于别的case子句重复:
           匹配后可以执行的语句
    default(对应最后的else,可选):
           匹配后可以执行的语句
}
public class SwitchNum {
    public static void main(String[] args) {
        int n = 99;

        String str = n + "对应的中文数字是:";
        switch (n){
            case 1:
                str+="壹·";
                break;
            case 2:
                str+="贰";
                break;
            case 3:
                str+="叁";
                break;
            case 4:
                str+="肆";
                break;
            case 5:
                str+="伍";
                break;
            case 6:
                str+="陆";
                break;
            case 7:
                str+="柒";
                break;
            case 8:
                str+="扒";
                break;
            case 9:
                str+="玖";
            default:
                System.out.println("错误的值"+n+"。值需要在大于等于1,小于等于9之间。");
        }
        System.out.println(str);
    }
}
# 相关链接:
[github地址](https://github.com/wangshuningyin/)
![个人公众号](https://img.haomeiwen.com/i6152450/4062304af51fa71b.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
上一篇 下一篇

猜你喜欢

热点阅读