java - 如何求和对象时间并创建总数?
问题描述
该程序的目的是提示用户输入活动时间,一旦用户决定结束应用程序,它会打印运行总时间并最终以分钟和秒为单位返回总时间。我遇到的问题是了解如何通过在类型 Itime 对象上调用 addTime 方法来添加 totalTime 和 activityTime。
司机班
public class ActivityManager
{
public static void main(String[] args)
{
Itime totalTime, activityTime; // declare totalTime and activityTime of type Itime
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
// 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.addTime(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
}
}
主班
public class Itime
{
private int minutes;
private double seconds;
/**
* Constructer objects of class Itime
*/
public Itime (int minutes, double seconds)
{
assert minutes >=0;
assert seconds >=0 && seconds <60;
}
/**
* Getter methods
*/
public int getMinutes() { return this.minutes; }
public double getSeconds() { return this.seconds; }
/**
* Method to return time in String format
*/
public String toString ()
{
String toString = minutes + " minutes and " + seconds + " seconds";
return toString;
}
**public addTime (pass Itime objects as params here)**
{
}
}
解决方案
尝试这个
public Itime addTime(Itime itime) {
this.minutes = this.minutes + itime.getMinutes();
this.seconds = this.seconds + itime.getSeconds();
if (this.seconds > 60) {
this.seconds = this.seconds % 60;
this.minutes++;
}
return this;
}
推荐阅读
- mysql - 如何使用一个触发器执行多个操作
- c++ - 用 string_view 构造 istringstream 不会编译
- javascript - 下拉列表的第一项未定义
- game-maker - 数字模数加法的函数(如度数)
- c# - 我怎样才能将ActionLink“添加图片”放在导航栏的中间?
- javascript - 如何向此代码添加一个函数,使我能够从下拉菜单中选择选项?
- python - 如何使用python从本地网站获取数据
- python - 从邻接矩阵中绘制具有边权重的特定网络
- jquery - 如何使 jquery 数据表呈现的搜索框可访问?
- android - 无法构建发布应用程序,gradle 说找不到封闭方法'boolean onCreateOptionsMenu(android.view.Menu)