Time() - 将时间设置为1200的默认构造函数.Time(int h,int m) - 如果h介于1和23之间,则将小时设置为h。否则,将小时设置为0.如果m介于0和59之间,则将分钟设置为m。否则,将分钟设置为0.时间应包括以下方法:
String toString() - 以格式:0819返回长度为4的字符串的时间。请注意,如果小时或分钟是一位数,则应首先打印零。例如,6应打印为06。
String convert() - 返回从军事时间转换为标准时间的字符串的时间。例如,0545变为早上5:45,而1306变为1:06 pm。
void increment() - 将时间提前一分钟。请记住,60分钟= 1小时。因此,如果您的时间是0359,并且您添加一分钟,它将变为0400. 2359应该增加到0000。 我的作业只是时间类而不是主要方法。 这是我写的(主要方法除外):
public class Time { private int hour; private int minute;
public Time() { hour = 12; minute = 0; this(12, 0); }
public Time(int h, int m) { hour = 0; minute = 0; if (h > 0 && h < 24) { hour = h; } if (m <= 59 && m >= 0) { minute = m; } }
public String toString() {
String hours = "";
String minutes = "";
if (hour < 10) {
hours += "0";
}
if (minute < 10) {
minutes = "0";
}
hours += hour;
minutes += minute;
return hours + minutes;
}
public String convert() {
String meridian;
if (hour >= 12) {
meridian = "pm";
} else {
meridian = "am";
}
String M1 = toString().substring(2,3);
String H1;
if ((hour - 12) > 0) {
H1 = "" + (hour - 12) + ":";
} else {
H1 = "" + (hour) + ":";
}
return (H1 + M1 + " " + meridian);
}
public void increment() {
minute ++;
if (minute == 60) {
hour += 1;
minute = 00;
}
if (hour == 24) {
hour = 00;
}
}
}
这是主要方法:
public static void main(String str[]) throws IOException
{
Time time1 = new Time(14, 56);
System.out.println("time1: " + time1);
System.out.println("convert time1 to standard time: " + time1.convert());
System.out.println("time1: " + time1);
System.out.print("increment time1 five times: ");
time1.increment();
time1.increment();
time1.increment();
time1.increment();
time1.increment();
System.out.println(time1 + "\n");
Time time2 = new Time(-7, 12);
System.out.println("time2: " + time2);
System.out.print("increment time2 67 times: ");
for (int i = 0; i < 67; i++)
time2.increment();
System.out.println(time2);
System.out.println("convert to time2 standard time: " + time2.convert());
System.out.println("time2: " + time2 + "\n");
Time time3 = new Time(5, 17);
System.out.println("time3: " + time3);
System.out.print("convert time3: ");
System.out.println(time3.convert());
Time time4 = new Time(12, 15);
System.out.println("\ntime4: " + time4);
System.out.println("convert time4: " + time4.convert());
Time time5 = new Time(0, 15);
System.out.println("\ntime5: " + time5);
System.out.println("convert time5: " + time5.convert());
Time time6 = new Time(24, 15);
System.out.println("\ntime6: " + time6);
System.out.println("convert time6: " + time6.convert());
Time time7 = new Time(23,59);
System.out.println("\ntime7: " + time7);
System.out.println("convert time7: " + time7.convert());
time7.increment();
System.out.println("increment time7: " + time7);
System.out.println("convert time7: " + time7.convert());
}
}
当我在我的代码运行器中运行它时,它说我没有通过任务。 我错过了什么? 这是我的输出
time1: 0000
convert time1 to standard time: 0am
time1: 0000
increment time1 five times: 0005
time2: 0000
increment time2 67 times: 0107
convert to time2 standard time: 8am
time2: 0107
time3: 0000
convert time3: 0am
time4: 0000
convert time4: 0am
time5: 0000
convert time5: 0am
time6: 0000
convert time6: 0am
time7: 0000
convert time7: 0am
increment time7: 0001
convert time7: 1am
这是预期的输出:
time1: 1456
convert time1 to standard time: 2:56 PM
time1: 1456
increment time1 five times: 1501
time2: 0012
increment time2 67 times: 0119
convert to time2 standard time: 1:19 AM
time2: 0119
time3: 0517
convert time3: 5:17 AM
time4: 1215
convert time4: 12:15 PM
time5: 0015
convert time5: 12:15 AM
time6: 0015
convert time6: 12:15 AM
time7: 2359
convert time7: 11:59 PM
increment time7: 0000
convert time7: 12:00 AM