hw2打卡

这周总算还是完成任务了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<d.year)return true;
 90       else if(this.year==d.year&&this.month<d.month)return true;
 91       else if(this.year==d.year&&this.month==d.month&&this.day<d.day)return true;
 92       else return false;
 93      }
 94   
 95   /** Determines whether this Date is after the Date d.
 96    *  @return true if and only if this Date is after d. 
 97    */
 98   public boolean isAfter(Date d) {
 99       if(isBefore(d)||(this.year==d.year&&this.month==d.month&&this.day==d.day))
100            return false;     
101       else return true;
102                      
103   }
104 
105   /** Returns the number of this Date in the year.
106    *  @return a number n in the range 1...366, inclusive, such that this Date
107    *  is the nth day of its year.  (366 is used only for December 31 in a leap
108    *  year.)
109    */
110   public int dayInYear() {
111       int num=0;
112       for(int i=0;i<this.month;i++)
113       num+=daysInMonth(i,this.year);
114       num+=this.day;
115       return num;                          
116   }
117 
118   /** Determines the difference in days between d and this Date.  For example,
119    *  if this Date is 12/15/2012 and d is 12/14/2012, the difference is 1.
120    *  If this Date occurs before d, the result is negative.
121    *  @return the difference in days between d and this date.
122    */
123   public int difference(Date d) {
124      if (isAfter(d)) {
125       int diff=0;
126        for(int i=d.year;i<this.year;i++) {
127          if(isLeapYear(i)) diff+=366;
128          else diff+=365;}
129          int diffday=this.dayInYear()-d.dayInYear();
130          return diff+diffday;
131      }
132      else if (isBefore(d)) {                           // replace this line with your solution
133      return -d.difference(this); 
134      }
135      else return 0;
136   } 
137 
138   public static void main(String[] argv) {
139     System.out.println("
Testing constructors.");
140     Date d1 = new Date(1, 1, 1);
141     System.out.println("Date should be 1/1/1: " + d1);
142     d1 = new Date("2/4/2");
143     System.out.println("Date should be 2/4/2: " + d1);
144     d1 = new Date("2/29/2000");
145     System.out.println("Date should be 2/29/2000: " + d1);
146     d1 = new Date("2/29/1904");
147     System.out.println("Date should be 2/29/1904: " + d1);
148 
149     d1 = new Date(12, 31, 1975);
150     System.out.println("Date should be 12/31/1975: " + d1);
151     Date d2 = new Date("1/1/1976");
152     System.out.println("Date should be 1/1/1976: " + d2);
153     Date d3 = new Date("1/2/1976");
154     System.out.println("Date should be 1/2/1976: " + d3);
155 
156     Date d4 = new Date("2/27/1977");
157     Date d5 = new Date("8/31/2110");
158 
159     /* I recommend you write code to test the isLeapYear function! */
160 
161     System.out.println("
Testing before and after.");
162     System.out.println(d2 + " after " + d1 + " should be true: " + 
163                        d2.isAfter(d1));
164     System.out.println(d3 + " after " + d2 + " should be true: " + 
165                        d3.isAfter(d2));
166     System.out.println(d1 + " after " + d1 + " should be false: " + 
167                        d1.isAfter(d1));
168     System.out.println(d1 + " after " + d2 + " should be false: " + 
169                        d1.isAfter(d2));
170     System.out.println(d2 + " after " + d3 + " should be false: " + 
171                        d2.isAfter(d3));
172 
173     System.out.println(d1 + " before " + d2 + " should be true: " + 
174                        d1.isBefore(d2));
175     System.out.println(d2 + " before " + d3 + " should be true: " + 
176                        d2.isBefore(d3));
177     System.out.println(d1 + " before " + d1 + " should be false: " + 
178                        d1.isBefore(d1));
179     System.out.println(d2 + " before " + d1 + " should be false: " + 
180                        d2.isBefore(d1));
181     System.out.println(d3 + " before " + d2 + " should be false: " + 
182                        d3.isBefore(d2));
183 
184     System.out.println("
Testing difference.");
185     System.out.println(d1 + " - " + d1  + " should be 0: " + 
186                        d1.difference(d1));
187     System.out.println(d2 + " - " + d1  + " should be 1: " + 
188                        d2.difference(d1));
189     System.out.println(d3 + " - " + d1  + " should be 2: " + 
190                        d3.difference(d1));
191     System.out.println(d3 + " - " + d4  + " should be -422: " + 
192                        d3.difference(d4));
193     System.out.println(d5 + " - " + d4  + " should be 48762: " + 
194                        d5.difference(d4));
195   }
196 }
View Code

运行结果:

程序运行成功之前曾经在一个地方卡了大半天。。。就是在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   }
View Code

也就是在 System.exit(0); 之前加了一行System.out.println("Input error!");想让它显示个错误

结果是这样的,也不会报错,正常运行,但是后面什么都不输出

百思不得其解。。。看到的大神求解答一波~~?

原文地址:https://www.cnblogs.com/jxtang/p/7192362.html