第一次作业+105032014036
(1)程序流程图

(2)测试用例设计
|
覆盖方式 |
用例号 |
输入 |
期待结果 |
实际结果 |
通过 |
时间 |
|
语句覆盖 |
1 |
2013.12.31 |
2014.1.1 |
2014.1.1 |
Yes |
|
|
语句覆盖 |
2 |
2090.12.31 |
年份超出范围 |
年份超出范围 |
Yes |
|
|
语句覆盖 |
3 |
2016.3.6 |
2016.3.7 |
2016.3.7 |
Yes |
|
|
判定覆盖 |
4 |
2090.13.35 |
年份超出范围 |
年份超出范围! 月份超出范围! 日期超出范围! 2080.13.35 |
Yes |
|
|
判定覆盖 |
5 |
2014.12.31 |
2015.1.1 |
2015.1.1 |
Yes |
|
|
条件覆盖 |
6 |
2017.2.3 |
2014.2.4 |
2017.3.1 |
N0 |
|
|
|
|
|||||
|
|
||||||
|
|
(4)代码优化建议
未对非闰年的二月进行判断,导致二月只有29天确被当成了30天来计算,下面给出我的代码优化
import java.util.Scanner;
public class test01 {
static String NextDate(int year,int month,int day){
if(((year%4==0&&year%100!=0)||year%400==0)&&month==2)
{
if(day==29){day=1;month++;}
else day++;
}
else if(month==12)
{
if(day==31){
day=1;
month=1;
year++;
}
else day++;
}
else if(month==1||month==3||month==5||month==7||month==8||month==10)
{
if(day==31)
{
day=1;
month++;
}
else day++;
}
else if(month==2||month==4||month==6||month==11||month==12&&day==30)
{
if(month==2&&day==28){
day=1;
month++;
}
else if(day==30)
{
day=1;
month++;
}
else day++;
}
else day++;
String date="后一天的日期是:"+year+"年"+month+"月"+day+"日";
return date;
}
public static boolean check(int y,int m,int d){
if(y<1915||y>2050){
System.out.println("年份超出范围");
return false;
}
else if(d<1||d>31||(m==2&&d>29)){
System.out.println("日期超出范围");
return false;
}
else if(m<1||m>12){
System.out.println("月份超出范围");
return false;
}
else {
return true;
}
}
//main方法
public static void main(String[] args) {
while(true){
System.out.println("----请输入日期---");
Scanner scanner=new Scanner(System.in);
System.out.print("请输入年份:");
int year=scanner.nextInt();
System.out.print("请输入月份:");
int month=scanner.nextInt();
System.out.print("请输入日期:");
int day=scanner.nextInt();
if(check(year, month, day)){
System.out.println(NextDate(year, month, day));
}
}
}
}
浙公网安备 33010602011771号