第三次作业+105032014067
一、流程图
二、测试用例设计
用例编号 | 覆盖方式 | 输入数据 | 预期结果 | 实际结果 | 测试结论 |
1 | 判定、语句 | 1900 1 23 | 年份超出范围 | 年份超出范围 | T |
2 | 判定、条件 | 2051 13 30 | 年份超出范围 | 年份超出范围 | T |
3 | 判定 、语句 | 1967 -3 16 | 月份超出范围 | 月份超出范围 | T |
4 | 判定 、语句 | 1967 4 34 | 日期超出范围 | 日期超出范围 | T |
5 | 判定、条件 | 1998 6 31 | 日期超出范围 | 日期超出范围 | T |
6 | 判定 | 1968 9 29 | 1968年9月30日 | 1968年9月30日 | T |
7 | 路径 | 2013 1 31 | 2013年2月1日 | 2013年2月1日 | T |
8 | 路径、判定 | 2015 3 28 | 2015年3月29日 | 2015年3月29日 | T |
9 | 条件 | 2001 4 30 | 2001年5月1日 | 2001年5月1日 | T |
10 | 判定 | 2034 11 3 | 2034年11月4日 | 2034年11月4日 | T |
11 | 组合、路径 | 2050 12 31 | 2051年1月1日 | 2051年1月1日 | T |
12 | 组合、判定 | 2017 12 5 | 2017年12月6日 | 2017年12月6日 | T |
13 | 组合 | 2000 2 29 | 2000年3月1日 | 2000年3月1日 | T |
14 | 条件、语句 | 1996 2 30 | 日期超出范围 | 日期超出范围 | T |
15 | 组合、语句 | 2000 2 28 | 2000年2月29日 | 2000年2月29日 | T |
16 | 路径 | 1987 2 29 | 日期超出范围 | 日期超出范围 | T |
17 | 条件、判定 | 1943 2 28 | 1943年3月1日 | 1943年3月1日 | T |
三、Junit:
结果图:
四、采用单元测试可以保证原代码的完整性,根据自己所要测试的进行编写代码,方便测试简洁步骤,不易出错。
源代码:
1 import java.util.Scanner; 2 public class Date { 3 public static void main(String[] args){ 4 5 System.out.print("请输入日期"); 6 Scanner input = new Scanner(System.in); 7 8 int year = input.nextInt(); 9 int month = input.nextInt(); 10 int day = input.nextInt(); 11 12 GetDate(year,month,day); 13 input.close(); 14 } 15 16 public static void GetDate(int year, int month, int day){ 17 18 if (!(year >= 1912 && year <= 2050)){ //判断年份 19 System.out.println("年份超出范围!"); 20 return; 21 } 22 23 if (month > 12 || month < 1){//判断月份 24 System.out.println("月份超出范围!"); 25 return; 26 } 27 28 if (day > 31 || day < 1){//判断日期 29 System.out.println("日期超出范围!"); 30 return; 31 } 32 if (month == 4 && day == 31 ){ 33 System.out.println("日期超出范围!"); 34 return; 35 } 36 if (month == 6 && day == 31){//判断日期 37 System.out.println("日期超出范围!"); 38 return; 39 } 40 if (month == 9 && day == 31){//判断日期 41 System.out.println("日期超出范围!"); 42 return; 43 } 44 if (month == 11 && day == 31){//判断日期 45 System.out.println("日期超出范围!"); 46 return; 47 } 48 49 switch (month){//计算第二天日期 50 case 1: 51 case 3: 52 case 5: 53 case 7: 54 case 8: 55 case 10: 56 if (day == 31){ 57 month++; 58 day = 1; 59 } 60 else day++; 61 break; 62 case 2: 63 if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){//计算闰年的日期 64 if (day > 29){ 65 System.out.println("日期超出范围!"); 66 return; 67 } 68 if (day == 29){ 69 month = 3; 70 day = 1; 71 } 72 else day++; 73 } 74 else{ 75 if (day > 28){ 76 System.out.println("日期超出范围!"); 77 return; 78 } 79 if (day == 28){ 80 month = 3; 81 day = 1; 82 } 83 else day++; 84 } 85 break; 86 case 4: 87 case 6: 88 case 9: 89 case 11: 90 if (day == 30){ 91 month++; 92 day = 1; 93 } 94 else day++; 95 break; 96 case 12: 97 if (day == 31){ 98 year++; 99 month = 1; 100 day = 1; 101 } 102 else day++; 103 break; 104 } 105 System.out.println(year+"年"+month+"月"+day+"日"); 106 } 107 }