软件测试--设计函数实现输入日期显示星期几
1.划分等价类:
2.运用等价类划分法设计测试用例
3.源程序代码
1 import java.text.ParseException; 2 import java.text.SimpleDateFormat; 3 import java.util.Calendar; 4 import java.util.Date; 5 import java.util.Scanner; 6 7 public class test1 { 8 public static void main(String[] args) { 9 String year = null;//年份 10 String month = null;//月份 11 String day = null;//日 12 boolean result=true; 13 boolean result1=true; 14 boolean result2=true; 15 //输入年月日 16 Scanner scanner1 = new Scanner(System.in); 17 Scanner scanner2 = new Scanner(System.in); 18 Scanner scanner3 = new Scanner(System.in); 19 20 21 //判断年份 22 while (result) { 23 try { 24 System.out.println("请输入年份:1900-2050:"); 25 year=scanner1.next(); 26 //强制转换成int类型 27 int year1 = Integer.parseInt(year); 28 result = PanduanYear(year1);//判断段年份 29 if(result==true){ 30 result=false; 31 }else { 32 result=true; 33 System.out.println("输入年份超出范围,请重新输入:"); 34 } 35 36 } catch (Exception e) { 37 System.out.println("年份输入有误,请输入正确的年份"); 38 39 } 40 } 41 42 43 //判断月份 44 while (result1) { 45 System.out.println("请输入月份1-12:"); 46 month = scanner2.next(); 47 try { 48 //强制转换成int类型 49 int month1 = Integer.parseInt(month); 50 result1 = PanduanMonth(month1); 51 if (result1 == true) { 52 result1 = false; 53 } else { 54 result1 = true; 55 System.out.println("输入月份超出范围,请重新输入:"); 56 } 57 } catch (Exception e) { 58 System.out.println("月份输入有误,请输入正确的月份"); 59 } 60 } 61 62 //判断日期 63 while (result2) { 64 System.out.println("请输入日:"); 65 day = scanner3.next(); 66 try { 67 //强制转换成int类型 68 int year1 = Integer.parseInt(year); 69 int month1 = Integer.parseInt(month); 70 int day1 = Integer.parseInt(day); 71 result2 = PanDay(year1, month1, day1); 72 if (result2 == true) { 73 result2=false; 74 }else { 75 result2 = true; 76 System.out.println("输入日期超出范围,请重新输入:"); 77 } 78 } catch (Exception e) { 79 System.out.println("日期输入有误,请输入正确的日期"); 80 } 81 } 82 83 SimpleDateFormat fmt = new SimpleDateFormat("dd MM yyyy"); 84 85 Date d = null; 86 try { 87 d = fmt.parse(day+" "+month+" "+year); 88 } catch (ParseException e) { 89 throw new RuntimeException(e); 90 } 91 92 Calendar cal = Calendar.getInstance(); 93 94 cal.setTime(d); 95 96 int weekDay = cal.get(Calendar.DAY_OF_WEEK); 97 98 switch(weekDay) { 99 case 1 : System.out.println("SUNDAY"); break; 100 case 2 : System.out.println("MONDAY"); break; 101 case 3 : System.out.println("TUESDAY"); break; 102 case 4 : System.out.println("WEDNESDAY"); break; 103 case 5 : System.out.println("THURSDAY"); break; 104 case 6 : System.out.println("FRIDAY"); break; 105 case 7 : System.out.println("SATURDAY"); break; 106 default: break; 107 } 108 } 109 //判断合理的年份 110 public static boolean PanduanYear (Integer year){ 111 return year >= 1900 && year <= 2050; 112 } 113 114 //判断月份是否正常 115 public static boolean PanduanMonth (Integer month){ 116 return month >= 1 && month <= 12; 117 } 118 119 120 121 //判断日期是否输入正常 122 public static boolean PanDay(Integer year,Integer month ,Integer day){ 123 int run=0;//0是平年,1是闰年 124 int m=0;//0是31天,1是30天,2是29天,3是28天 125 boolean result = false; 126 if(year%400==0||year%4==0){ 127 run=1; 128 } 129 if(month==4||month==6||month==9||month==11){ 130 m=1; 131 } 132 if(month==2&&run==1){ 133 m=2; 134 } 135 if (month==2&&run==0){ 136 m=3; 137 } 138 139 if (m==1&&day>=1&&day<=30){ 140 result=true; 141 } 142 143 if (m==0&&day>=1&&day<=31){ 144 result=true; 145 } 146 147 if (m==2&&day>=1&&day<=29){ 148 result=true; 149 } 150 151 if (m==3&&day>=1&&day<=28){ 152 result=true; 153 } 154 155 return result; 156 } 157 158 159 160 }