A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Sayman 中级黑马   /  2015-10-31 15:40  /  304 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.Scanner;
  2. class GetDate {
  3.         public static void main(String[] args) {
  4.                 Scanner sc = new Scanner(System.in);
  5.                 System.out.println("请分别输入年,月,日");
  6.                 int year = sc.nextInt();
  7.                 int mouth = sc.nextInt();
  8.                 int date = sc.nextInt();
  9.                 if (getDate(year,mouth,date) == -1) {
  10.                         System.out.println("输入错误");
  11.                 }
  12.                 System.out.println(year + "年" + mouth + "月" + date + "日" + "是" + year + "年的第" + getDate(year,mouth,date) + "天");
  13.         }

  14.         public static int getDate(int year,int mouth,int date) {
  15.                 int num = 0;
  16.                 int[] arr = {0,31,28,31,30,31,30,31,31,30,31,30};                //只需要定义前11个月的天数
  17.                 if (year % 400 == 0 || (year % 4 == 0) && ((year % 100) != 0)) {        //判断是否为闰年
  18.                         arr[2] = 29;
  19.                 }
  20.                 try {
  21.                         for (int i = 0; i < mouth; i++) {       
  22.                         num += arr[i];                                                        //计算前面几个月的天数总和
  23.                         }
  24.                 }
  25.                 catch (Exception e) {
  26.                         System.out.println("不存在此月份" + e);
  27.                         return -1;
  28.                 }
  29.                 return num + date;
  30.         }
  31. }
复制代码


在输入第13个月时,检测出异常,还有错误的返回值,这个地方请问怎样设置返回语句?

评分

参与人数 1黑马币 +10 收起 理由
TTsu + 10

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马