[英]How to properly use a toString method?
因此,我在计算机科学实验室项目上处于领先地位,基本上,我们正在创建活动时间跟踪器。 该程序的目标是在控制台中输入分钟和秒,然后程序将使用这些值并打印总时间,重复此过程,直到用户输入一个无效的数字为止,程序将在此终止。
ActivityManager类的大部分内容都是由教授完成的,由教授创建,仅需更改几行,并带有注释。
这是该类:
{
public static void main(String[] args)
{
// declare totalTime and activityTime of type Itime
Itime totalTime, activityTime;
int minutes; double seconds; // user input values
Scanner input = new Scanner (System.in); // to read user input
// display purpose and author
System.out.println ("This program tracks total time in minutes");
System.out.println ("and seconds for a series of activities.");
System.out.println (); // print blank line
System.out.println ("Author: ");
System.out.println (); // print blank line
// specify format for input
System.out.println ("Enter activity time in minutes and"
+ " seconds, all in a");
System.out.println ("single line with spaces in between. Entering"
+ " values" );
System.out.println ("outside appropriate ranges will terminate"
+ " the program.");
System.out.println (); // print blank line
// create the totalTime object of type Itime with 0 minutes and 0.0 seconds
totalTime = new Itime(0,0.0);
System.out.println ("Total time so far is: "
+ totalTime.toString());
System.out.println (); // print blank line
// prompt and read time for an activity
System.out.print ("Enter time for an activity: ");
minutes = input.nextInt();
seconds = input.nextDouble();
// Accumulate if appropriate
while (minutes >= 0 && seconds >= 0 && seconds < 60) {
// create the activityTime object of type Itime with given minutes and seconds
activityTime = new Itime(minutes,seconds);
// add totalTime and activityTime and put the result in totalTime
totalTime = totalTime.addItime(activityTime);
System.out.println ("Total time so far is: "
+ totalTime.toString());
System.out.println (); // print blank line
// prompt and read time for another activity
System.out.print ("Enter time for an activity: ");
minutes = input.nextInt();
seconds = input.nextDouble();
}
// wrap up and print final total
System.out.println ("Sentinel received");
System.out.println (); // print blank line
System.out.println ("Total time so far is: "
+ totalTime.toString());
System.out.println (); // print blank line
// print closing remarks
System.out.println ("Program has terminated.");
System.out.println (); // print blank line
}
我们的任务是通过创建一个Itime类来完成项目,该类在这里:
{
private int minutes;
private double seconds;
public Itime(int minutes, double seconds){
assert(minutes >= 0 && seconds >= 0 && seconds < 60);
}
public int getMinutes(){return minutes;}
public double getSeconds(){return seconds;}
public String toString(){
Itime totalTime = new Itime(minutes, seconds);
String toString=this.minutes +" minutes and "+this.seconds+" seconds";
return toString;}
public Itime addItime(Itime bTime){
int totalMinutes = this.minutes+bTime.getMinutes();
double totalSeconds = this.seconds+bTime.getSeconds();
if(totalSeconds>60){
totalSeconds-=60;
totalMinutes+=1;
}
Itime totalTime = new Itime(totalMinutes,totalSeconds);
return totalTime;
}
我在这个项目中遇到的问题是,无论输入什么值,它都会返回默认的起始值0分钟和0.0秒。 最初,我认为我的问题是addItime方法,但是在查看代码后,我认为问题在于toString方法之内。
控制台将打印出以下内容:
该程序以分钟和秒为单位跟踪一系列活动的总时间。
作者:版本:2019年1月25日
以分钟和秒为单位输入活动时间,所有活动都在一行中,中间有空格。 输入超出适当范围的值将终止程序。
到目前为止,总时间为:0分钟0.0秒
输入活动时间:20 20
到目前为止,总时间为:0分钟和0.0秒
输入活动时间:
有什么建议么?
欢迎使用堆栈溢出。 (之所以这样说,是因为我认为这是您第一次使用它,因为您在问题描述中附加了所有代码。对于以后的问题,请尝试仅关注您认为问题所在的代码。无论如何,我希望您喜欢我们的社区)。
现在让我们集中在这里:
public String toString() {
Itime totalTime = new Itime(minutes, seconds);
String toString=this.minutes +" minutes and "+this.seconds+" seconds";
return toString;
}
首先,您不需要创建Itime
类的另一个实例,因为您已经可以访问toString()
方法中的变量。 同样,此方法也不会损坏。 工作正常。
现在,检查构造函数:
public Itime(int minutes, double seconds) {
assert(minutes >= 0 && seconds >= 0 && seconds < 60);
}
您永远不会为ITime
类的实例分配minutes
或seconds
。 这就是为什么当您尝试打印任何给定的实例(通过控制台,我可能会猜到)时,总是会得到默认值(零)的原因。
构造函数应该看起来像这样:
public Itime(int minutes, double seconds) {
assert(minutes >= 0 && seconds >= 0 && seconds < 60);
this.minutes = minutes;
this.seconds = seconds;
}
希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.