2013-11-09 71 views
1

嗨,我得到了标题错误与此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); 
} 

你能帮助我吗?我尝试了很多方法来解决这个错误,但没有任何帮助,谢谢。

+2

年龄无效。如果你想做这样的比较,它应该返回一个数字(int,double等)。 –

+0

您想比较整年或全年的年龄,例如:以秒/ milis为单位? – arkonautom

回答

0
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的比较方法,并返回日期/年龄比较的结果。

+0

非常感谢它) –

4

错误发生的事情,因为你正在试图比较两个void使用(S)>运营商在下面的语句:

if(age(ob2) > age(ob)) 

age方法返回void这里提到:

public static void age(Ship ob) 

你可能应该从年龄返回一个整数值,这将使您的比较合乎逻辑。

+0

感谢您的快速答案,但是当我在年龄()从void类型更改为int时,洞函数不起作用,是的,然后我可以使用比较(),但年龄()不工作; x –

0

Your'e比较两个无效返回值。您应该在age方法中返回一个整数,并可能更改其逻辑。

+0

感谢您的快速答案,但是当我在年龄()时将类型从void更改为int时,洞函数不起作用,是的,然后我可以使用compare(),但age()不工作; x –

相关问题