【发布时间】:2014-10-09 21:31:17
【问题描述】:
我有一个任务给我一个预先编写的用于 Date 类和 LibraryBook 类的驱动程序,我必须编写这些类,以便它们通过驱动程序中的一堆检查。我的问题来自其中一项检查,它查看我是否会因按时归还一本书而被收费。显然我应该被收取 0 美元,但出于某种原因,我的程序显示为 15 美元。 罚款的计算方法如下:
public double getFineAmount(Date dateReturned){
double fine;
Date dueDate = this.getDueDate();
if(dateReturned.isOnOrBefore(dueDate)){
fine = 0.00;
}
else{
if(this.isFiction){
fine = 1.2 * dueDate.getDaysUntil(dateReturned);
if(fine > 15){
fine = 15.00;
}
}
else{
fine = 1.5 * dueDate.getDaysUntil(dateReturned);
if(fine > 20){
fine = 20.00;
}
}
}
return fine;
}
当图书借阅日期是 2012 年 2 月 10 日,而到期日期是 2012 年 3 月 2 日时,由于某种原因,isOnOrBefore 方法返回 false。这是 isOnOrBefore 方法:
public boolean isOnOrBefore(Date other){
boolean onOrBefore;
Scanner sc = new Scanner(other.toString()).useDelimiter("-");
int otherDay = sc.nextInt();
int otherMonth = sc.nextInt();
int otherYear = sc.nextInt();
if(otherYear >= this.year){
if(otherMonth >= this.month){
if(otherDay >= this.day){
onOrBefore = true;
}
else{
onOrBefore = false;
}
}
else{
onOrBefore = false;
}
}
else{
onOrBefore = false;
}
return onOrBefore;
}
我认为问题是由闰年引起的,但我看不出是什么导致了错误。这是检测闰年的代码,以防万一
public boolean isLeapYear(){
boolean leapYear;
if(this.year % 100 == 0){
if(this.year % 400 == 0){
leapYear = true;
}
else{
leapYear = false;
}
}
else{
if(this.year % 4 == 0){
leapYear = true;
}
else{
leapYear = false;
}
}
return leapYear;
}
如果需要,我可以发布更多代码。
【问题讨论】:
标签: java if-statement methods nested