--- title: "00-判断语句" created: 2025-12-02 tags: - 项目 aliases: - 判断语句 --- # 判断语句 ## if-else语句 与C++、Python中类似。 例如: ```java package com.yxc; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int year = sc.nextInt(); if (year % 100 == 0) { if (year % 400 == 0) System.out.printf("%d是闰年\n", year); else System.out.printf("%d不是闰年\n", year); } else { if (year % 4 == 0) System.out.printf("%d是闰年\n", year); else System.out.printf("%d不是闰年\n", year); } } } ``` ## switch语句 与C++中类似。 ```java package com.yxc; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int day = sc.nextInt(); String name; switch (day) { case 1: name = "Monday"; break; case 2: name = "Tuesday"; break; case 3: name = "Wednesday"; break; case 4: name = "Thursday"; break; case 5: name = "Friday"; break; case 6: name = "Saturday"; break; case 7: name = "Sunday"; break; default: name = "not valid"; } System.out.println(name); } } ``` ## 逻辑运算符与条件表达式 与C++、Python类似。 例如: ```java package com.yxc; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int year = sc.nextInt(); if (year % 100 != 0 && year % 4 == 0 || year % 400 == 0) System.out.printf("%d是闰年\n", year); else System.out.printf("%d不是闰年\n", year); } } ``` 注意一点是 java中 int不能作为bool值来判断 所以不能写成 year % 100 && year % 4== 0 || year % 400 == 0 ## 练习题 [[2-Learning/05-项目/04-SpringBoot框架课/01-java语法/02-判断语句/01-倍数.md|倍数]] [[05-零食|零食]] [[03-区间|区间]] [[04-游戏时间|游戏时间]] [[02-动物|动物]] --- **项目分区导航**: [[05-钞票|钞票]] ⬅️ | 00-判断语句 | ➡️ [[01-倍数|倍数]]