博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hw2打卡
阅读量:6607 次
发布时间:2019-06-24

本文共 3840 字,大约阅读时间需要 12 分钟。

这周总算还是完成任务了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
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

你可能感兴趣的文章
卫语句学习
查看>>
【php】对PHPExcel一些简单的理解
查看>>
文档统一用Word编写之Word写&发送邮件(Office2007)
查看>>
JavaScript的简单继承实现案例
查看>>
第六篇 VIM你值得拥有!
查看>>
项目管理学习笔记之八.课程总结
查看>>
setjmp与longjmp的分析
查看>>
generate ascii table
查看>>
MATLAB绘制3D隐函数曲面的几种方法
查看>>
jquery改变链接移上光标时的颜色实例
查看>>
2013吉林通化邀请赛 1005 GCD and LCM
查看>>
高淇java300集JAVA常用类作业
查看>>
oracle大型数据库系统在AIX/unix上的实战详解 这本书要二版
查看>>
关于JAVA中的static方法、并发问题以及JAVA运行时内存模型
查看>>
Direct3D 初涉: 颜色
查看>>
jquery的checked以及disabled
查看>>
XLua系统学习
查看>>
Scala提取器Extractor实战详解之Scala学习笔记-19
查看>>
vue--初识
查看>>
<Linux命令行学习 第一节> CentOS在虚拟机的安装
查看>>