花几千上万学习Java,真没必要!(七)

swtich语句:

测试代码1:

java 复制代码
package testswitch.com;
 //根据月份和年份,当月份是 2 时,检查年份是否为闰年,然后继续执行下一个 case,打印出"三月",然后终止switch 语句。
public class MainSwitch {
    public static void main(String[] args) {
        int month = 2;
        int year = 2024;

        switch (month) {
            case 1:
                System.out.println("一月");
                break;
            case 2:
                System.out.println("二月");
                if (year % 4 == 0) {
                    System.out.println("这是闰年的二月");
                }
                // 注意:没有 break,将继续执行下一个 case
            case 3:
                System.out.println("三月");
                break;
            case 4:
                System.out.println("四月");
                break;
            default:
                System.out.println("其他月份");
                break;
        }
    }
}

运行结果如下:

测试代码2:

java 复制代码
package testswitch.com;
import java.util.Scanner;
public class LeapYearExample {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		System.out.print("请输入年份:");
		int year = scanner.nextInt();

		System.out.print("请输入月份:");
		int month = scanner.nextInt();

		if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
			System.out.println(year + "年是闰年");
		} else {
			System.out.println(year + "年不是闰年");
		}

		switch (month) {
		case 1:
			System.out.println("一月有31天");
			break;
		case 2:
			if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
				System.out.println("二月有29天");
			} else {
				System.out.println("二月有28天");
			}
			break;
		case 3:
			System.out.println("三月有31天");
			break;
		case 4:
			System.out.println("四月有30天");
			break;
		case 5:
			System.out.println("五月有31天");
			break;
		case 6:
			System.out.println("六月有30天");
			break;
		case 7:
			System.out.println("七月有31天");
			break;
		case 8:
			System.out.println("八月有31天");
			break;
		case 9:
			System.out.println("九月有30天");
			break;
		case 10:
			System.out.println("十月有31天");
			break;
		case 11:
			System.out.println("十一月有30天");
			break;
		case 12:
			System.out.println("十二月有31天");
			break;
		default:
			System.out.println("无效的月份");
			break;
		}
	}
}

运行结果如下:

测试代码3:

java 复制代码
package testswitch.com;
import java.util.Scanner;
//使用一个while循环实现选择操作。
//输入数字选择不同的操作,包括计算两个数字的和、判断一个数的奇偶性以及退出程序。
//根据选择,程序会进行相应的处理并输出结果,直到退出为止。
public class InPutSwitch {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        boolean running = true;

        while (running) {
            System.out.println("请选择操作:");
            System.out.println("1. 计算两个数字的和");
            System.out.println("2. 判断一个数的奇偶性");
            System.out.println("3. 退出");

            int choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    System.out.println("请输入第一个数字:");
                    int num1 = scanner.nextInt();
                    System.out.println("请输入第二个数字:");
                    int num2 = scanner.nextInt();
                    int sum = num1 + num2;
                    System.out.println("两个数字的和为:" + sum);
                    break;
                case 2:
                    System.out.println("请输入一个数字:");
                    int number = scanner.nextInt();
                    if (number % 2 == 0) {
                        System.out.println(number + " 是偶数");
                    } else {
                        System.out.println(number + " 是奇数");
                    }
                    break;
                case 3:
                    running = false;
                    System.out.println("谢谢使用,再见!");
                    break;
                default:
                    System.out.println("无效的选择,请重新输入");
            }
        }
    }
}

运行结果如下:

测试代码4:case穿透

java 复制代码
package testswitch.com;
//case 穿透执行多个case 分支的代码块。
//当变量 day 的值为1、2或3时,它将执行与这三个 case 相关联的代码块,因为它们没有被 break 所终止。
public class SwitchTest {
    public static void main(String[] args) {
        int day = 3;

        switch (day) {
            case 1:
            case 2:
            case 3:
                System.out.println("星期一、二或三");
                break;
            case 4:
            case 5:
                System.out.println("星期四或五");
                break;
            default:
                System.out.println("其他");
                break;
        }
    }
}

测试代码5:case穿透

java 复制代码
package testswitch.com;
import java.util.Scanner;
//switch语句的case穿透。
public class CasePenetration{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入月份:");
        int month = scanner.nextInt();

        scanner = new Scanner(System.in);
        System.out.print("请输入年份:");
        int year = scanner.nextInt();

        switch (month) {
            case 2:
                if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                    System.out.println(year + "年是闰年");
                } else {
                    System.out.println(year + "年不是闰年");
                }
            case 3:
                System.out.println("执行月份为3的代码块");
                break;
            // 继续添加其它月份的case...
            default:
                System.out.println("输入的月份无效");
                break;
        }
    }
}

运行结果如下:

相关推荐
西岸行者3 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
悠哉悠哉愿意3 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
别催小唐敲代码3 天前
嵌入式学习路线
学习
毛小茛3 天前
计算机系统概论——校验码
学习
babe小鑫3 天前
大专经济信息管理专业学习数据分析的必要性
学习·数据挖掘·数据分析
winfreedoms3 天前
ROS2知识大白话
笔记·学习·ros2
在这habit之下3 天前
Linux Virtual Server(LVS)学习总结
linux·学习·lvs
我想我不够好。3 天前
2026.2.25监控学习
学习
im_AMBER3 天前
Leetcode 127 删除有序数组中的重复项 | 删除有序数组中的重复项 II
数据结构·学习·算法·leetcode
CodeJourney_J3 天前
从“Hello World“ 开始 C++
c语言·c++·学习