花几千上万学习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;
        }
    }
}

运行结果如下:

相关推荐
巧克力味的桃子8 分钟前
进制转换3 学习笔记
笔记·学习
Purple Coder20 分钟前
人工智能学习路线
学习
小帅学编程27 分钟前
Spring(侧重注解开发)
java·学习·spring
爱喝水的鱼丶35 分钟前
SAP-ABAP:在SAP世界里与特殊字符“斗智斗勇”:一份来自实战的避坑指南
运维·服务器·数据库·学习·sap·abap·特殊字符
科技林总41 分钟前
【系统分析师】认证介绍
学习
不吃橘子的橘猫1 小时前
NVIDIA DLI 《Build a Deep Research Agent》学习笔记
开发语言·数据库·笔记·python·学习·算法·ai
刘孬孬沉迷学习1 小时前
层与天线的区别
网络·学习·5g·信息与通信·mimo·预编码·层映射
冬夜戏雪1 小时前
【学习日记】【12.30】【14/60】
服务器·网络·学习
AI视觉网奇2 小时前
火星- ue数字人智能体 学习笔记
人工智能·笔记·学习
深蓝海拓2 小时前
PySide6从0开始学习的笔记(二十) qdarkstyle的深入应用
笔记·python·qt·学习·pyqt