这周总算还是完成任务了hhh
运行代码:
1 package hw2; 2 /* Date.java */ 3 4 import java.io.*; 5 6 public class Date { 7 private int month; 8 private int day; 9 private int year; 10 /** Constructs a date with the given month, day and year. If the date is 11 * not valid, the entire program will halt with an error message. 12 * @param month is a month, numbered in the range 1...12. 13 * @param day is between 1 and the number of days in the given month. 14 * @param year is the year in question, with no digits omitted. 15 */ 16 public Date(int month, int day, int year) { 17 if(isValidDate(month,day,year)){ 18 this.month=month; 19 this.day=day; 20 this.year=year; 21 } 22 else 23 System.exit(0); 24 } 25 /** Constructs a Date object corresponding to the given string. 26 * @param s should be a string of the form "month/day/year" where month must 27 * be one or two digits, day must be one or two digits, and year must be 28 * between 1 and 4 digits. If s does not match these requirements or is not 29 * a valid date, the program halts with an error message. 30 */ 31 public Date(String s) { 32 if(s.matches("\\d{1,2}\\/\\d{1,2}\\/\\d{1,4}")){ 33 String[]a=s.split("/"); 34 month=Integer.parseInt(a[0]); 35 day=Integer.parseInt(a[1]); 36 year=Integer.parseInt(a[2]); 37 }else 38 System.exit(0); 39 } 40 41 /** Checks whether the given year is a leap year. 42 * @return true if and only if the input year is a leap year. 43 */ 44 public static boolean isLeapYear(int year) { 45 if((year%4==0&&year%100!=0)||(year%400==0)) { 46 return true; } 47 else return false; 48 } 49 50 /** Returns the number of days in a given month. 51 * @param month is a month, numbered in the range 1...12. 52 * @param year is the year in question, with no digits omitted. 53 * @return the number of days in the given month. 54 */ 55 public static int daysInMonth(int month, int year) { 56 switch(month) { 57 case 4:case 6:case 9:case 11: 58 return 30; 59 case 2:if(isLeapYear(year))return 29;else return 28; 60 default: 61 return 31; 62 } 63 } 64 65 /** Checks whether the given date is valid. 66 * @return true if and only if month/day/year constitute a valid date. 67 * 68 * Years prior to A.D. 1 are NOT valid. 69 */ 70 public static boolean isValidDate(int month, int day, int year) { 71 if(year>0&&day<=daysInMonth(month,year)&&month<=12){ 72 return true;} 73 else return false; 74 } 75 76 /** Returns a string representation of this date in the form month/day/year. 77 * The month, day, and year are expressed in full as integers; for example, 78 * 12/7/2006 or 3/21/407. 79 * @return a String representation of this date. 80 */ 81 public String toString() { 82 return(this.month+"/"+this.day+"/"+this.year); 83 } 84 85 /** Determines whether this Date is before the Date d. 86 * @return true if and only if this Date is before d. 87 */ 88 public boolean isBefore(Date d) { 89 if(this.year
运行结果:
程序运行成功之前曾经在一个地方卡了大半天。。。就是在public Date(int month, int day, int year)这个地方,
我之前写的代码
1 public Date(int month, int day, int year) { 2 if(isValidDate(month,day,year)){ 3 this.month=month; 4 this.day=day; 5 this.year=year; 6 } 7 else 8 System.out.println("Input Error!"); 9 System.exit(0); 10 }
也就是在 System.exit(0); 之前加了一行System.out.println("Input error!");想让它显示个错误
结果是这样的,也不会报错,正常运行,但是后面什么都不输出
百思不得其解。。。看到的大神求解答一波~~?