第2次作业+105032014158
1.测试帖连接:http://www.cnblogs.com/wxcclub/p/6581040.html
2.测试人员发现的缺陷:
该代码只通过判断月份和天数是否为最后一月的一天的情况,没有考虑年份为闰年2月份只有29天以及4,6,9,11月只有30天的情况,
并且年份月份日期超出范围依然输出了错误日期,综上,该程序并未通过测试。
3.修正后的java代码:
1 import java.util.*; 2 public class ruance_1 { 3 4 public static String Msg(int year, int month, int day) 5 { 6 String msg; 7 if((year<1912||year>2050)) //判断年份是否超出 8 { 9 msg = "年份超出范围!"; 10 return msg; 11 } 12 if((month<1||month>12)) // 判断月份是否超出 13 { 14 msg = "月份超出范围!"; 15 return msg; 16 } 17 //判断日期是否超出 18 if(month==4||month==6||month==9||month==11) 19 { 20 if((day<1||day>30)) 21 { 22 msg = "日期超出范围!"; 23 return msg; 24 } 25 } 26 else if(month==2) 27 { 28 if((year%4==0&&year%100!=0)||year%400==0)//闰年 29 { 30 if((day<1||day>29)) 31 { 32 msg = "日期超出范围!"; 33 return msg; 34 } 35 } 36 else //非闰年 37 { 38 if((day<1||day>28)) 39 { 40 msg = "日期超出范围!"; 41 return msg; 42 } 43 } 44 } 45 else 46 { 47 if((day<1||day>31)) 48 { 49 msg = "日期超出范围!"; 50 return msg; 51 } 52 } 53 msg=NextDate(year,month,day); 54 return msg; 55 } 56 //实现下一日的方法 57 public static String NextDate(int year, int month, int day) 58 { 59 String nextdate; 60 if(month==2) //2月 61 { 62 if(((year%4==0&&year%100!=0)||year%400==0)) //闰年时的2月 63 { 64 if(day==29) 65 { 66 day=1; 67 month++; 68 } 69 else 70 { 71 day++; 72 } 73 } 74 else 75 { 76 if(day==28) 77 { 78 day=1; 79 month++; 80 } 81 else 82 { 83 day++; 84 } 85 } 86 } 87 else if(month==12) //12月份 88 { 89 if(day==31) 90 { 91 year++; 92 day=1; 93 month=1; 94 } 95 else 96 { 97 day++; 98 } 99 } 100 //一般月份 101 else 102 { 103 if(month==4||month==6||month==9||month==11) 104 { 105 if(day==30) 106 { 107 month++; 108 day=1; 109 } 110 else 111 { 112 day++; 113 } 114 } 115 else 116 { 117 if(day==31) 118 { 119 month++; 120 day=1; 121 } 122 else 123 { 124 day++; 125 } 126 } 127 128 } 129 nextdate = "" + year + "年" + "" + month + "" + "月" + day + "日" ; 130 return nextdate; 131 } 132 133 public static void main(String[] args) { 134 // TODO Auto-generated method stub 135 int a=1; 136 while(a==1) 137 { 138 int year, month, day; 139 System.out.println("请依次输入年份,月份,日期!"); 140 Scanner input = new Scanner(System.in); 141 year = input.nextInt(); 142 month = input.nextInt(); 143 day = input.nextInt(); 144 System.out.println("输入的日期为:"); 145 System.out.println(year + "年" + month + "月" + day + "日"); 146 System.out.println("输出为:"); 147 System.out.println(Msg(year, month, day)); 148 System.out.println("是否继续?1/0"); 149 a = input.nextInt(); 150 } 151 } 152 }
4.缺陷原因:
没有考虑实际的需求,只是按照题目表面的意思执行任务,导致没有实用性,并且画蛇添足,没有简洁明了表现出输入的错误。
4.变更:
本次修改,主要是将对日期的判断单独写成一个函数,其次对于题目的要求再加上一点日常应有的限制,比如闰年与非闰年的差异。较之前的代码,还改进了原本日期超出范围还会输出错误日期的行为。
5.体会:
要尽量结合实际完成任务。