该程序由以下两个类组成,应该打印时间
在类Melons
内,Eclipse在String.format
方法下给出了一个错误,我不明白为什么
public class Melons {
private int hour;
private int minute;
private int second;
public void setTime(int h, int m, int s)
{
hour = ((h>=0 || h<24) ? h : 0);
minute = ((m>=0 || m<60) ? m : 0);
second = ((s>=0 || s<24) ? s : 0);
}
public String toString()
{
// Problem here
return String.format("%d:%02d:%02d %s",
((hour==0||hour==12) ? 12 : hour%12),
minute,
second,
(hour<12 ? "AM" : "PM"));
}
}
class Apples
{
public static void main(String args[])
{
Melons melonsObject = new Melons();
System.out.println(melonsObject.toString());
melonsObject.setTime(13, 35, 9);
System.out.println(melonsObject.toString());
}
}