我目前正在尝试构建一个数据类型Time(),它与主类的输入交互,在12小时时钟上创建一个时间(例如输入(16,47),会给你4:47 PM )这些方法可以对通常被认为无效的输入数据进行调整,使其再次准确(例如,(28,-148)的输入值将从评估为28小时后的2小时28分钟移除(25,32)并且25将评估为1,给出时间为1:32 AM)。根据这个驱动程序,我从目前创建的内容中得到了错误的值。
package newProject;
/**
* A program to test the Time class.
*
*
*/
public class TestTime {
public static void main(String[] args) {
System.out.println("Basic Times\n"
+ "-----------");
testTime(12, 30, 12, 30, "12:30 PM");
testTime(16, 16, "4:00 PM");
testTime(0, 0, 0, 0, "12:00 AM");
testTime(0, 10, 0, 10, "12:10 AM");
testTime(1, 5, 1, 5, "1:05 AM");
testTime(11, 59, 11, 59, "11:59 AM");
testTime(12, 12, "12:00 PM");
testTime(12,1,12,1, "12:01 PM");
testTime(13,55,13,55, "1:55 PM");
testTime(23,55,23,55, "11:55 PM");
System.out.println();
System.out.println("Invalid Times\n"
+ "-------------");
testTime(24, 0, "12:00 AM");
testTime(49, 10, 1, 10, "1:10 AM");
testTime(49, 125, 3, 5, "3:05 AM");
testTime(-1, 23, "11:00 PM");
testTime(0, -1, 23, 59, "11:59 PM");
testTime(-27, 21, "9:00 PM");
testTime(10, -125, 7, 55, "7:55 AM");
testTime(-55, -308, 11, 52, "11:52 AM");
System.out.println();
System.out.println("addMinutes\n"
+ "----------");
testAdd(0, 0, 20, 0, 20, "12:20 AM");
testAdd(0, 0, 90, 1, 30, "1:30 AM");
testAdd(0, 40, 30, 1, 10, "1:10 AM");
testAdd(0, 40, 90, 2, 10, "2:10 AM");
testAdd(23, 50, 11, 0, 1, "12:01 AM");
testAdd(23, 50, 3 * 24 * 60 + 1, 23, 51, "11:51 PM");
System.out.println();
}
/**
* Test whether Time constructor, getters, and print
* are behaving properly when zero minutes.
*
* @param hrs what hour to say to the constructor
* @param finalHrs what hour the Time should end up with
* @param rep how that Time should print out
*/
private static void testTime(int hrs,
int finalHrs,
String rep) {
System.out.print("new Time(" + hrs + ", 0) --> " + finalHrs + ", 0: ");
Time t = new Time(hrs, 0);
if (t.getHour() == finalHrs && t.getMinute() == 0)
System.out.println("PASS");
else
System.out.println("FAIL");
t.print();
System.out.println(" should be " + rep);
}
/**
* Test whether Time constructor, getters, and print are
* behaving properly when minutes are not (necessarily) 0.
*
* @param hrs what hour to give the constructor
* @param mins what minute to give the constructor
* @param finalHrs what hour the Time should end up with
* @param finalMins what minute the Time should end up with
* @param rep how the Time should print out
*/
private static void testTime(int hrs, int mins,
int finalHrs, int finalMins,
String rep) {
System.out.print("new Time(" + hrs + ", " + mins + ") --> "
+ finalHrs + ", " + finalMins + ": ");
Time t = new Time(hrs, mins);
if (t.getHour() == finalHrs && t.getMinute() == finalMins)
System.out.println("PASS");
else
System.out.println("FAIL");
t.print();
System.out.println(" should be " + rep);
}
/**
* Check whether addMinutes, getters, and print are working properly.
* @param hrs what hour to give the constructor
* @param mins what minute to give the constructor
* @param addMins the number of minutes to add to the Time
* @param finalHrs what hour the Time should end up with
* @param finalMins what minute the Time should end up with
* @param rep how the (later) Time should print out
*/
private static void testAdd(int hrs, int mins, int addMins,
int finalHrs, int finalMins,
String rep) {
Time t = new Time(hrs, mins);
Time t2 = t.addMinutes(addMins);
System.out.print("Adding " + addMins + " minutes to ");
t.print();
System.out.print(" --> ");
if (t2.getHour() == finalHrs && t2.getMinute() == finalMins)
System.out.println("PASS");
else
System.out.println("FAIL");
t2.print();
System.out.println(" should be " + rep);
}
}
到目前为止,这是我为修改这些所需的方法而整理的内容,但我真的不知道下一步该去哪里。
package newProject;
public class Time {
private final int MAX_HOURS = 23;
private final int MIN_HOURS = 0;
private final int MAX_MINUTES = 59;
private final int MIN_MINUTES = 0;
private int hour;
private int minute;
private Time() {
}
public Time(int hrs, int mins) {
this.hour = hrs;
this.minute = mins;
}
private void fixMe() {
if (minute > MAX_MINUTES) {
this.minute %= minute/60;
this.hour = minute/60 + hour;
}
if (minute < MIN_MINUTES) {
this.minute = minute + 60;
}
if (hour > MAX_HOURS) {
this.hour %= hour/24;
}
if(hour < MIN_HOURS) {
this.hour = hour + 24;
}
}
public int getHour() {
return this.hour;
}
public int getMinute() {
return this.minute;
}
public Time addMinutes(int mins) {
Time t = new Time();
t.fixMe();
return t;
}
public void print() {
if (hour == 0) {
hour = 12;
}
if (hour > 1 && hour <= 12) {
System.out.print(hour);
} else {
System.out.print(hour - 12);
}
if (minute < 10) {
System.out.print(":0" + minute);
} else {
System.out.print(minute);
}
if (hour <= 11) {
System.out.print(" AM");
} else
System.out.print(" PM");
}
}
这些是我得到的输出,远远不是我想要的。
run:
Basic Times
-----------
new Time(12, 30) --> 12, 30: PASS
1230 PM should be 12:30 PM
new Time(16, 0) --> 16, 0: PASS
4:00 PM should be 4:00 PM
new Time(0, 0) --> 0, 0: PASS
12:00 PM should be 12:00 AM
new Time(0, 10) --> 0, 10: PASS
1210 PM should be 12:10 AM
new Time(1, 5) --> 1, 5: PASS
-11:05 AM should be 1:05 AM
new Time(11, 59) --> 11, 59: PASS
1159 AM should be 11:59 AM
new Time(12, 0) --> 12, 0: PASS
12:00 PM should be 12:00 PM
new Time(12, 1) --> 12, 1: PASS
12:01 PM should be 12:01 PM
new Time(13, 55) --> 13, 55: PASS
155 PM should be 1:55 PM
new Time(23, 55) --> 23, 55: PASS
1155 PM should be 11:55 PM
Invalid Times
-------------
new Time(24, 0) --> 0, 0: FAIL
12:00 PM should be 12:00 AM
new Time(49, 10) --> 1, 10: FAIL
3710 PM should be 1:10 AM
new Time(49, 125) --> 3, 5: FAIL
37125 PM should be 3:05 AM
new Time(-1, 0) --> 23, 0: FAIL
-13:00 AM should be 11:00 PM
new Time(0, -1) --> 23, 59: FAIL
12:0-1 PM should be 11:59 PM
new Time(-27, 0) --> 21, 0: FAIL
-39:00 AM should be 9:00 PM
new Time(10, -125) --> 7, 55: FAIL
10:0-125 AM should be 7:55 AM
new Time(-55, -308) --> 11, 52: FAIL
-67:0-308 AM should be 11:52 AM
addMinutes
----------
Adding 20 minutes to 12:00 PM --> FAIL
12:00 PM should be 12:20 AM
Adding 90 minutes to 12:00 PM --> FAIL
12:00 PM should be 1:30 AM
Adding 30 minutes to 1240 PM --> FAIL
12:00 PM should be 1:10 AM
Adding 90 minutes to 1240 PM --> FAIL
12:00 PM should be 2:10 AM
Adding 11 minutes to 1150 PM --> FAIL
12:00 PM should be 12:01 AM
Adding 4321 minutes to 1150 PM --> FAIL
12:00 PM should be 11:51 PM
最终,如果该计划按其应有的方式运作,他们都将评估为通过。任何帮助将不胜感激
答案 0 :(得分:0)
我不确定您现有的fixMe
方法应该如何工作,但这是一个有效的实现:
private void fixMe() {
hours += minutes / 60;
minutes %= 60;
if (minutes < 0) { // if minutes was negative,
minutes += 60; // correct the result of the modulus 60 to make it positive
hours--; // correct the result of the /60 by subtracting one more hour
}
hours %= 24;
if (hours < 0) { // if hours was negative,
hours += 24; // correct the modulus 24 to be positive
}
}
这基本上只是模数/整数除法,以使值进入正确的范围。诀窍是处理负面投入;在这些情况下,模数将为负,整数除法的结果将比您想要的多一个。纠正这些因素,并按预期工作。
另外,请确保您的addMinutes(int)
方法实际上会增加分钟数。
答案 1 :(得分:0)
通过在类中内部表示所有时间为秒,并且仅在必要时将此内部表示转换为小时和分钟,您可以使数学更简单(因此更不容易出错)。这样,您的addMinutes()
方法只需要进行简单的添加:
public void addMinutes(int m) {
this.seconds += m * 60;
}
getHour()
和getMinute()
方法需要做一些简单的数学运算来确定正确的小时和分钟,但另一方面,你可以用方法来扩展你的课程,以获得一天,月,年和秒。
实际上,在编程中实现时间和日期格式的常用方法是存储自特定时间点以来经过的秒数。如果您有兴趣,可以read more about that here。
另外,您可能想查看现有的Java date and time APIs。