- import java.util.Scanner;
- class GetDate {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.println("请分别输入年,月,日");
- int year = sc.nextInt();
- int mouth = sc.nextInt();
- int date = sc.nextInt();
- if (getDate(year,mouth,date) == -1) {
- System.out.println("输入错误");
- }
- System.out.println(year + "年" + mouth + "月" + date + "日" + "是" + year + "年的第" + getDate(year,mouth,date) + "天");
- }
- public static int getDate(int year,int mouth,int date) {
- int num = 0;
- int[] arr = {0,31,28,31,30,31,30,31,31,30,31,30}; //只需要定义前11个月的天数
- if (year % 400 == 0 || (year % 4 == 0) && ((year % 100) != 0)) { //判断是否为闰年
- arr[2] = 29;
- }
- try {
- for (int i = 0; i < mouth; i++) {
- num += arr[i]; //计算前面几个月的天数总和
- }
- }
- catch (Exception e) {
- System.out.println("不存在此月份" + e);
- return -1;
- }
- return num + date;
- }
- }
复制代码
在输入第13个月时,检测出异常,还有错误的返回值,这个地方请问怎样设置返回语句? |