我正在寻求一些帮助,以使用涉及用户输入的测试类完成 Time2 的分配。任务是: 修改 Time2 类(如下)以将时间实现为自午夜以来的秒数。该类应该有一个数据字段(一个整数,表示自午夜以来的秒数)而不是三个。此更改不应影响公共(public)方法的参数、行为或输出。 使用 main 方法创建一个 Driver 类来测试您的 Time2 类。该程序应要求用户输入午夜后的小时数、分钟数和秒数,创建一个 Time2 对象并使用 mutator 方法。然后程序应使用 toString() 方法打印出时间。 我相信唯一需要修改的是 3 个整数——小时、分钟、秒——变成一个整数 (totalseconds) 和 set/get 方法。我相信我的计算是正确的,但我的测试课是我绊脚石的地方。我想知道 toString 方法和 toUniversalString 之间是否存在问题。我能够让测试类请求用户输入,但它一直输出 12:00:00AM。
public class Time2 {
private int totalseconds;
public Time2(int hour, int minute, int second)setTime
{
this.totalseconds = (hour * 3600);
this.totalseconds += (minute * 60);
this.totalseconds += (second);
}
public Time2(Time2 time)setTime
{
this(time.getHour(), time.getMinute(), time.getSecond());
}
public void setTime(int hour, int minute, int second)
{
if (hour < 0 || hour >= 24)
throw new IllegalArgumentException("Hour must be 0-23");
if (minute < 0 || minute >= 59)
throw new IllegalArgumentException("Minute must be 0-59");
if (second < 0 || second >= 59)
throw new IllegalArgumentException("Hour must be 0-59");
this.totalseconds = (hour * 3600);
this.totalseconds += (minute * 60);
this.totalseconds += (second);
}
public void setHour(int hour)
{
if (hour < 0 || hour >= 24)
throw new IllegalArgumentException("Hour must be 0-23");
this.totalseconds = (hour * 3600);
}
public void setMinute(int minute)
{
if (minute < 0 || minute >= 59)
throw new IllegalArgumentException("Minute must be 0-59");
this.totalseconds = (minute * 60);
}
public void setSecond(int second)
{
if (second < 0 || second >= 24)
throw new IllegalArgumentException("Second must be 0-59");
this.totalseconds = (second);
}
public int getHour()
{
return totalseconds / 3600;
}
public int getMinute()
{
return (totalseconds - (3600 * getHour())) / 60;
}
public int getSecond()
{
return totalseconds - (3600 * getHour()) - (60 * getMinute());
}
public String toUniversalString()
{
return String.format(
"%02d:%02d:%02d", getHour(), getMinute(), getSecond());
}
public String toString()
{
return String.format("%d:%02d:%02d %s",((getHour() == 0 || getHour() ==
12) ? 12 : getHour() % 12), getMinute(), getSecond(), (getHour()
< 12 ? "AM" : "PM"));
}
}
测试类
import java.util.Scanner;
public class Time2Test {
public static void main(String[] args) { // instantiate CommissionEmployee object
Scanner input = new Scanner(System.in);
System.out.print("Enter hour:");
String hour = input.next();
Integer.parseInt(hour);
System.out.print("Enter minute:");
String minute = input.next();
Integer.parseInt(minute);
System.out.print("Enter second:");
String second = input.next();
Integer.parseInt(second);
Time2 clock = new Time2(0, 0, 0);
System.out.printf(clock.toString());
}
}
最佳答案
您使用了错误的构造函数。您有一系列伸缩构造函数,它们似乎没有任何更大的用途,只是让您自己感到困惑。您像这样创建一个新的 Clock
实例:
Time2 Clock = new Time2();
你真正想要的是你的另一个构造函数:
public Time2(int hour, int minute, int second)
{
this.totalseconds = (hour * 3600);
this.totalseconds = (minute * 60);
this.totalseconds = (second);
}
但是,在调用它之前,请注意它接受 int 输入,因此您将不得不使用 parseInt
或 valueOf
解析来自扫描仪的 String 输入.在任何一种情况下,您都可以获得必须捕获的 NumberFormatException。
之后,请注意您的构造函数缺少对 setTime()
方法的调用。您将获得 12:00:00,因为您从未使用用户输入设置时间。
然后...再次注意,在您的构造函数中,您有一系列赋值运算符 (=
),实际上,分和秒的运算符应该是 +=
.
这些东西都不是真正的技术,但它很草率。有时,当你陷入困境时,你需要走开,做点别的事情。然后回去用新的眼光检查你的代码。坚持下去,但要休息一下。这些东西很快就会成为第二天性。
注意:不要以大写字母开头命名实例变量,例如 Clock
。首字母大写保留给实际的类名。保持实例名称的驼峰式(小写第一个字母,变量名称中第二个和后续单词的大写字母 likeThis。
关于java - Time2Test(带用户输入),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59340193/