java的编程之旅7——switch选择语句


switch(/**整型、字符或者字符串表达式**/){
    case 值1:
    case 值2: [break;]
    case 值n:
    default:
}

根据switch 括号内的值分别与case后的值做匹配,如果匹配成功则执行case后的语句,程序如果没有遇到break,程序将向下执行,并且不再匹配其他case值

break的用法:

当程序遇到break,可跳出当前语句块

default的用法:

当匹配完所有的case分支之后,如果仍未发现匹配成功,则执行default后的语句,类似if...else语句中else的用法

例1:判断成绩等级(switch方法)


public class practice {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入分数:");
        int score = sc.nextInt();
        switch(score/10){
            case 10: case 9:
                System.out.println("成绩为A");
                break;
            case 8:
                System.out.println("成绩为B");
                break;
            case 7:
                System.out.println("成绩为C");
                break;
            case 6:
                System.out.println("成绩为D");
                break;
            default:
                System.out.println("不及格");

        }
    }
}

例2:基础计算器


public class Calculate {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入第一个数:");
        int num1 = sc.nextInt();
        System.out.println("输入运算符(+、-、*、/):");
        String c = sc.next();
        System.out.println("输入第二个数:");
        int num2 = sc.nextInt();
        switch(c){
            case "+":
                System.out.println(num1+"+"+num2+"="+(num1+num2));
                break;
            case "-":
                System.out.println(num1+"-"+num2+"="+(num1-num2));
                break;
            case "*":
                System.out.println(num1+"*"+num2+"="+(num1*num2));
                break;
            case "/":                  //考虑除数不能为0的情况
                if(num2==0){
                    System.out.println("除数不能为0!");
                    System.exit(0);
                }else{
                    System.out.println(num1+"/"+num2+"="+(num1/num2));
                }
        }
    }
}

例3:根据年份判断月份有几天


public class judgeDay {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入年份:");
        int year = sc.nextInt();
        System.out.println("输入月份:");
        int month = sc.nextInt();
        if(month<0||month>12){
            System.out.println("月份非法~");
            System.exit(0);
        }else{
            int day = 0;
            switch(month){
                case 1: case 3: case 5: case 7: case 8: case 10: case 12:
                    day = 31;break;
                case 4: case 6: case 9: case 11:
                    day = 30;break;
                case 2:
                    day=((year%4==0||year%100!=0)||year%100==0)?29:28;
                    break;
            }
            System.out.printf("%d年%d月有%d天", year, month, day);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

惊爆点大男主

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值