嗨,我收到此Java代码的标题错误
public static void age(Ship ob){
DateTime myBirthDate = ob.getDate();
DateTime now = new DateTime();
Period period = new Period(myBirthDate, now);
System.out.print("Ship age is " + ob.getName() + " е " );
PeriodFormatter formatter = new PeriodFormatterBuilder()
.appendYears().appendSuffix(" years")
.appendMonths().appendSuffix(" months")
.appendWeeks().appendSuffix(" weeks")
.appendDays().appendSuffix(" days")
.appendHours().appendSuffix(" hours")
.appendMinutes().appendSuffix(" mnutes")
.appendSeconds().appendSuffix(" seconds\n ")
.printZeroNever().toFormatter();
String elapsed = formatter.print(period);
System.out.println(elapsed);
}
public static void compare(Ship ob, Ship ob2) throws ParseException {
if(age(ob2) > age(ob)){ //<---- I get the Error here , when i try to comapre two objects
System.out.println( "The ship,wich is more years is " + ob2);
} else
System.out.println( "The ship,wich is more years is " + ob);
}
你能帮助我吗?我尝试了多种方法来修复此错误,但没有帮助,谢谢。
最佳答案
int compareResult = ob.getDate().compareTo(ob2.getDate());
if (compareResult > 0) //ob2 date after ob date
else if (compareResult < 0) //ob2 date before ob
else //ob2 has same date as ob
如果要练习OOP,像这样计算年龄是不好的。
您应该使年龄方法成为Ship类的成员。和/或在Ship类中具有一个比较方法,该方法可以接受另一艘Ship并返回日期/年龄比较的结果。
关于java - 未为参数类型定义运算符>,无效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19874826/