03_Switch例子

根据月份输出对应季节

package com.it.learn.switch_test;

import java.util.Scanner;

public class SwitchTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入1-12的月份:");
        int month = sc.nextInt();
        switch (month) {
            case 12:
            case 1:
            case 2:
                System.out.println(month + "月是冬季");
                break;
            case 3:
            case 4:
            case 5:
                System.out.println(month + "月是春季");
                break;
            case 6:
            case 7:
            case 8:
                System.out.println(month + "月是夏季");
                break;
            case 9:
            case 10:
            case 11:
                System.out.println(month + "月是秋季");
                break;
            default:
                System.out.println("你输入的不是正确的月份");
        }
    }
}

划分学生成绩等级

这里使用了隐式转换

package com.it.learn.switch_test;

import java.util.Scanner;

public class SwitchTest02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入0-100的分数:");
        int score = sc.nextInt();
        if (score >= 0 && score <= 100) {
            switch (score / 10) {
                case 10:
                case 9:
                    System.out.println(score + "分是A");
                    break;
                case 8:
                    System.out.println(score + "分是B");
                    break;
                case 7:
                    System.out.println(score + "分是C");
                    break;
                case 6:
                    System.out.println(score + "分是D");
                    break;
                default:
                    System.out.println(score + "分是E");
            }
        } else {
            System.out.println("你输入的分数有误");
        }
    }
}

posted @ 2023-07-06 10:42  徐林俊  阅读(8)  评论(0编辑  收藏  举报