花几千上万学习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 小时前
设计模式学习整理
学习·设计模式
湘-枫叶情缘3 小时前
解构认知边界:论万能方法的本体论批判与方法论重构——基于跨学科视阈的哲学-科学辩证
科技·学习·重构·生活·学习方法
inputA5 小时前
【LwIP源码学习6】UDP部分源码分析
c语言·stm32·单片机·嵌入式硬件·网络协议·学习·udp
海尔辛5 小时前
学习黑客5 分钟读懂Linux Permissions 101
linux·学习·安全
真的想上岸啊6 小时前
学习51单片机01(安装开发环境)
嵌入式硬件·学习·51单片机
每次的天空7 小时前
Android学习总结之Glide自定义三级缓存(面试篇)
android·学习·glide
名誉寒冰7 小时前
# KVstorageBaseRaft-cpp 项目 RPC 模块源码学习
qt·学习·rpc
开发游戏的老王8 小时前
[虚幻官方教程学习笔记]深入理解实时渲染(An In-Depth Look at Real-Time Rendering)
笔记·学习·虚幻
码农小嘉9 小时前
若依框架页面
学习
小新1109 小时前
微信小程序学习之轮播图swiper
学习·微信小程序·notepad++