https://www.luogu.com.cn/problem/P5716https://www.luogu.com.cn/problem/P5716
import java.time.YearMonth;
import java.util.*;
public class Main{
public static void main (String[] args){
Scanner sc =new Scanner(System.in);
int y =sc.nextInt();
int m =sc.nextInt();
YearMonth ym=YearMonth.of(y, m);
int days=ym.lengthOfMonth();
System.out.print(days);
}
}
import java.time.YearMonth;
public class Main {
public static void main(String[] args) {
int year = 2024; // 你想要查询的年份
int month = 3; // 你想要查询的月份
YearMonth yearMonth = YearMonth.of(year, month);
int daysInMonth = yearMonth.lengthOfMonth();
System.out.println("Year: " + year + ", Month: " + month + ", Days: " + daysInMonth);
}
}
1. `YearMonth.of(year, month);` - 这行代码使用指定的年份和月份创建了一个 `YearMonth` 对象。`YearMonth` 类表示了一个特定的年份和月份,它不包含具体的日期,只包含了年份和月份信息。
2. `yearMonth.lengthOfMonth();` - 这行代码调用了 `YearMonth` 对象的 `lengthOfMonth()` 方法,该方法返回了指定年份和月份对应的天数。因为不同月份的天数是不同的,所以这个方法会返回特定月份的天数。
通过这两行代码,你可以获取到指定年份和月份的天数。