酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

java方法功能为输入三个变量,分别代表年、月、日,返回一个字符串,为输入日期的下一天,如输入(2022,7,3),返回值为“2022.7.5”

以下是一个实现所述功能的 Java 方法示例: ```java public class NextDay { public static String getNextDay(int year, int month, int day) { int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (isLeapYear(year)) { daysInMonth[1] = 29; // 闰年的2月有29天 } if (day < 1 || day > daysInMonth[month - 1]) { return "输入的日期无效"; // 输入日期无效 } else if (day == daysInMonth[month - 1]) { if (month == 12) { // 年底 return (year + 1) + ".1.1"; } else { // 月底 return year + "." + (month + 1) + ".1"; } } else { // 下一天 return year + "." + month + "." + (day + 1); } } public static boolean isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } public static void main(String[] args) { System.out.println(getNextDay(2022, 7, 3)); // 输出:2022.7.4 } } ``` 在上述示例中,`getNextDay` 方法接收输入的年、月、日作为参数,并根据给定的年份判断是否为闰年。然后,检查输入日期是否有效,如果无效则返回错误消息。如果日期是所在月份的最后一天,则返回下一个月的第一天。否则,返回给定日期的下一天。在 `main` 方法中,以示例参数调用 `getNextDay` 方法并打印结果。 [2024-10-13 16:46:07 | AI写代码神器 | 930点数解答]

相关提问