第二次作业+105032014161

  1. 测试帖链接:http://www.cnblogs.com/zenghongyu/p/6602604.html
  2. 测试人员提出的问题、发现的缺陷:
    1. 代码中缺少对大小月份的判断,如4月份是小月,不存在31日,应输出错误提示。
    2. 代码中缺少对输入值得类型的判断,如当用户输入非整形数值时,应输出错误提示。
    3. 代码中尽量添加注释。

    4. 尽量才有采用的括号形式,方便阅读。

    5. Java可以采用try catch来处理用户输入非整形数值产生的异常。
  3. 修正后的代码清单
    1. Date.java类
 1 public class Date {
 2     public int year;
 3     public int month;
 4     public int day;
 5     
 6     public Date() {
 7         super();
 8     }
 9     public int getYear() {
10         return year;
11     }
12     public void setYear(int year) {
13             this.year = year;
14     }
15     public int getMonth() {
16         return month;
17     }
18     public void setMonth(int month) {
19             this.month = month;
20     }
21     public int getDay() {
22         return day;
23     }
24     public void setDay(int day) {
25             this.day = day;
26     }
27     //next day方法
28     public String NextDay(){
29         //判断大月且不是12月31号
30         if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 ||( month == 12 && day<31))
31         {
32             if (day < 31)
33                 day++;
34             else if (day == 31)
35             {
36                 month++;
37                 day =1;
38             }
39 
40         }
41         //判断12月31号
42         else if (month == 12 && day == 31)
43         {
44             year++;
45             month = 1;
46             day = 1;
47         }
48         //判断除了2月的小月
49         else if (month == 4 || month == 6 || month == 9 || month == 11)
50         {
51             if (day < 30)
52                 day++;
53             else if (day == 30)
54             {
55 
56                 month++;
57                 day = 1;
58             }
59             else{
60                 return "日期错误!";
61             }
62 
63         }
64         //2月需判断是否闰年
65         else  if (month == 2)
66         {
67             if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){
68                 if (day < 29)
69                     day++;
70                 else if (day == 29)
71                 {
72                     month++;
73                     day = 1;
74                  }
75                 else
76                 {
77                     return "日期错误!";
78                 }
79             }
80             else{
81                  if (day < 28)
82                      day++;
83                  else if (day == 28)
84                  {
85                      month++;
86                      day = 1;
87                  }
88                  else{
89                      return "日期错误!";
90                  }
91             }
92         }
93         return "第二天:"+year+"年"+month+"月"+day+"日";
94     }
95     
96 }

    2.Test.java测试类:

 1 import java.util.Scanner;
 2 public class Test {
 3     @SuppressWarnings("resource")
 4     public static void main(String[] args){
 5         Date date=new Date();
 6         int year=0;
 7         int month=0;
 8         int day=0;
 9         while(true){
10             System.out.println("请输入日期:");
11             Scanner sc = new Scanner(System.in);
12             //判断输入的数是否整型数据
13             try{
14                 System.out.print("年:");
15                 year = sc.nextInt();
16                 System.out.print("月:");
17                 month=sc.nextInt();
18                 System.out.print("日:");
19                 day=sc.nextInt();
20             }catch (Exception e) {
21                 System.out.println("您所输入的数不是整数!");
22                 continue;
23             }
24             //判断日期是否在规定范围内
25             if(year>2050||year<1912)
26             {
27                 System.out.println("年份超出范围!\n");
28                 continue;
29             }
30             if(month>12||month<1)
31             {
32                 System.out.println("月份超出范围!\n");
33                 continue;
34 
35             }
36             if(day>31||day<1)
37             {
38                 System.out.println("日期超出范围!\n");
39                 continue;
40             }
41             //赋值
42             date.setYear(year);
43             date.setMonth(month);
44             date.setDay(day);
45             //输出next day
46             System.out.println(date.NextDay() );
47             //只要输入-1退出,输入其他就继续
48             System.out.println("\n********************\n是否继续?1 or -1:");
49             sc.nextLine();
50             String conn=sc.nextLine();
51             if(conn.equals("-1")){
52                 break;
53             }
54         }
55         System.out.println("感谢使用!");
56     }
57 
58 }

 

     4.修正后心得体会:

根据测试人员提出的代码缺陷和不足,对程序进行了改进,对小月没有31号的问题做了修改,采用try catch对非整型数据进行异常的捕获并提示。

posted @ 2017-03-27 15:38  wowowowww  阅读(244)  评论(0编辑  收藏  举报