我的代码没有显示正确或所需的输出时遇到问题。这是我写的代码。
import java.time.LocalDateTime;
public class DriversLicense
{
private String name;
private int id;
private int expYear;
private int expMonth;
private int expDay;
public DriversLicense(String name, int id, int expYear, int expMonth, int expDay)
{
this.name = name;
this.id = id;
this.expYear = expYear;
this.expMonth = expMonth;
this.expDay = expDay;
}
public boolean isExpired()
{
LocalDateTime date = LocalDateTime.now();
boolean tORf = false;
int year = date.getYear();
int month = date.getMonthValue();
int day = date.getDayOfMonth();
if(year > this.expYear && month > this.expMonth && day > this.expDay)
{
return true;
}
return tORf;
}
public void displayInfo()
{
System.out.println("Name: " + this.name);
System.out.println("ID: " + this.id);
System.out.println("Expiration: " + this.expYear + "/" + this.expMonth + "/" + this.expDay);
}
}
在我的isExpired()方法中,它应该检查天气当前日期是否晚于 ID 的到期日期。如果 ID 过期,它应该打印出 true,否则它应该打印出 false。出于某种原因,当我的程序检查的三个 id 应该是 false true false 时,我得到了所有的错误。这是我下面的测试文件。
public class TestDL
{
public static void main(String[] args)
{
DriversLicense dr1 = new DriversLicense("John Smith", 192891, 6, 21, 2018);
dr1.displayInfo();
System.out.println("Expired? " + dr1.isExpired());
System.out.println();
DriversLicense dr2 = new DriversLicense("Jennifer Brown", 728828, 5, 31, 2017);
dr2.displayInfo();
System.out.println("Expired? " + dr2.isExpired());
System.out.println();
DriversLicense dr3 = new DriversLicense("Britney Wilson", 592031, 7, 15, 2019);
dr3.displayInfo();
System.out.println("Expired? " + dr3.isExpired());
System.out.println();
}
}
这也是我目前得到的输出。
Name: John Smith
ID: 192891
Expiration: 6/21/2018
Expired? false
Name: Jennifer Brown
ID: 728828
Expiration: 5/31/2017
Expired? false
Name: Britney Wilson
ID: 592031
Expiration: 7/15/2019
Expired? false