给出某一天(年,月,日),计算出它的下一天,取值围为: 年:1000<= year <=3000 月:1<=month <=12 日: 1<= day <=31 如 1999 年 3 月 4 日的下一天是:1999 年 3 月 5 日 要求: 输入:三个参数(年,月,日) 输出:如能正确计算,计算出它的下一天, 否则,输出相应的错误信息。 1) 使用 java 语言编写程序实现
时间: 2024-02-06 09:09:22 浏览: 20
以下是实现该功能的 Java 代码:
```java
import java.util.Calendar;
import java.util.Scanner;
public class NextDayCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入年份(1000<=year<=3000):");
int year = scanner.nextInt();
System.out.print("请输入月份(1<=month<=12):");
int month = scanner.nextInt();
System.out.print("请输入日期(1<=day<=31):");
int day = scanner.nextInt();
if (year < 1000 || year > 3000) {
System.out.println("年份输入错误!");
return;
}
if (month < 1 || month > 12) {
System.out.println("月份输入错误!");
return;
}
if (day < 1 || day > 31) {
System.out.println("日期输入错误!");
return;
}
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, day);
calendar.add(Calendar.DAY_OF_MONTH, 1);
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH) + 1;
day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println("下一天为:" + year + "年" + month + "月" + day + "日");
}
}
```
运行程序后,输入年、月、日即可计算出下一天的日期。如果输入的年、月、日不符合要求,则会输出相应的错误信息。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)