测试结果链接:http://www.cnblogs.com/rcher/p/6592510.html
问题答复:
给出的绝大部分测试例子均能通过,改进建议只有一条,即对非法输入的判定,我认为这并不是原题目中功能要求的一部分,保证输入数据格式正确是前提条件,所以并未修改,而只是用java重构了一下代码。
1 package main; 2 import java.util.Scanner; 3 4 public class run { 5 public static int month[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 6 public static int checkVal(int y,int m,int d)//check whether the inputs are legal or not 7 { 8 if(m<1||m>12) 9 return 1; 10 if(d<1||d>month[m]) 11 return 2; 12 if(y<1912||y>2050) 13 return 3; 14 return 0; 15 } 16 17 public static int checkLeapYear(int y) ////check if y year is leap year return 1 otherwise return 0 18 { 19 if(y%4==0&&y%100!=0) 20 return 1; 21 if(y%100==0&&y%400==0) 22 return 1; 23 return 0; 24 } 25 public static int checkLastday(int y,int m,int d)//check whether the date is the last day of a year,if it is return 1 otherwise return 0 26 { 27 if(m==12&&d==31) 28 return 1; 29 else 30 return 0; 31 } 32 33 public static String getString(int y,int m,int d)//generate date in string form 34 { 35 return y+"年"+m+"月"+d+"日"; 36 } 37 38 public static String nextdate(int y,int m,int d)//calculate next date, result is presented in "xxxx年x月x日" form 39 { 40 if(checkLeapYear(y)==1) 41 { 42 month[2]=29; 43 } 44 switch (checkVal(y,m,d)) 45 { 46 case 1: 47 return "月份超出范围"; 48 case 2: 49 return "日期超出范围"; 50 case 3: 51 return "年份超出范围"; 52 default: 53 if(d<month[m]&&checkLastday(y,m,d)==0) 54 { 55 return getString(y, m, d+1); 56 } 57 if(d==month[m]) 58 { 59 if(checkLastday(y, m, d)==1) 60 { 61 return getString(y+1, 1, 1); 62 } 63 else 64 { 65 return getString(y, m+1, 1); 66 } 67 } 68 return ""; 69 } 70 } 71 public static void main (String[] args) 72 { 73 while(true) 74 { 75 System.out.println("请输入日期"); 76 int y,m,d; 77 Scanner input=new Scanner(System.in); 78 y=input.nextInt(); 79 if(y==-1) 80 break; 81 else 82 { 83 m=input.nextInt(); 84 d=input.nextInt(); 85 System.out.println(nextdate(y,m,d)); 86 month[2]=28; 87 } 88 } 89 } 90 }
时间: 2024-12-17 13:09:47