首页 > 解决方案 > 如何求和对象时间并创建总数?

问题描述

该程序的目的是提示用户输入活动时间,一旦用户决定结束应用程序,它会打印运行总时间并最终以分钟和秒为单位返回总时间。我遇到的问题是了解如何通过在类型 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)**
{

}

}

标签: javatimeinvoke

解决方案


尝试这个

  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;
  }

推荐阅读