当前位置: 首页 > 工具软件 > switchClass > 使用案例 >

Switch练习

屠钊
2023-12-01
// 提示用户输入年月日信息,判断这一天是这一年中的第几天并打印。

import java.util.Scanner;

public class SwichTest01 {
    public static void main(String[] args) {

        System.out.println("请输入年月日信息:");
        Scanner input = new Scanner(System.in);
        int years = input.nextInt();
        int months = input.nextInt();
        int days = input.nextInt();
        int sumDays = 0;

        switch (months){
            case 12 : sumDays += 30;
            case 11 : sumDays += 31;
            case 10 : sumDays += 30;
            case 9 : sumDays += 31;
            case 8 : sumDays += 31;
            case 7 : sumDays += 30;
            case 6 : sumDays += 31;
            case 5 : sumDays += 30;
            case 4 : sumDays += 31;
            case 3 : if ((years%4 == 0 && years%100 != 0) || (years%400 == 0)){
                sumDays += 29;
            }else {
                sumDays += 28;
            }
            case 2 : sumDays += 31;
            case 1 : sumDays += days;
        }
        System.out.println("今天是"+years+"年的第"+sumDays+"天");
    }
}
//输出月份和星期
import java.util.Scanner;

public class Test05 {
    public static void main(String[] args) {

        System.out.println("请输入一个数字:");
        Scanner input = new Scanner(System.in);
        int num1 = input.nextInt();

        switch (num1) {
            case 1:
                System.out.println("星期一");
                break;
            case 2:
                System.out.println("星期二");
                break;
            case 3:
                System.out.println("星期三");
                break;
            case 4:
                System.out.println("星期四");
                break;
            case 5:
                System.out.println("星期五");
                break;
            case 6:
                System.out.println("星期六");
                break;
            case 7:
                System.out.println("星期七");
                break;
            default:
                System.out.println("不合法");
        }

        System.out.println("请输入一个数字:");
        int num2 = input.nextInt();

        switch (num2) {
            case 1:
                System.out.println("一月");
                break;
            case 2:
                System.out.println("二月");
                break;
            case 3:
                System.out.println("三月");
                break;
            case 4:
                System.out.println("四月");
                break;
            case 5:
                System.out.println("五月");
                break;
            case 6:
                System.out.println("六月");
                break;
            case 7:
                System.out.println("七月");
                break;
            case 8:
                System.out.println("八月");
                break;
            case 9:
                System.out.println("九月");
                break;
            case 10:
                System.out.println("十月");
                break;
            case 11:
                System.out.println("十一月");
                break;
            case 12:
                System.out.println("十二月");
                break;
            default:
                System.out.println("不合法");

        }
    }
}
 类似资料: