2017-08-17 32 views
0

我想改变我的time2类从3个整数到一个整数来节省空间。我已经摆脱了小时,分钟和秒钟,现在只需要几秒钟来表示自午夜以来的时间。我在集合中取代了它,并且在3参数构造函数中获取了方法,并且它在Time2Test应用程序中运行,但只读秒。所有三个参数都被读作秒,所以我不确定发生了什么。除了Time2Test时间应该是12:25:42而不是42:42:42。首先是Time2类,然后我也添加了Time2Test以便清晰。任何方向将不胜感激。Java的时间2从3个整数到一个整数

public class Time2 { 
    private int totalseconds; 
    //no argument constructor 
    public Time2() 
    { 
     this(0,0,0); //invoke constructor with three arguments default to 0 
    } 

    //constructor with hour supplied minute and second default to 0 
    public Time2(int hour) 
    { 
     this(hour, 0, 0); //invoke constructor with 3 args 
    } 

    //constructor with hour and minute supplied seconds default to 0 
    public Time2(int hour, int minute) 
    { 
     this(hour, minute, 0); //invoke constructor with 3 args 
    } 

    //Time2 constructor with hour minute and second supplied also tests 

    public Time2(int hour, int minute, int second) 
    {  
     this.totalseconds = (hour * 3600); 
     this.totalseconds = (minute * 60); 
     this.totalseconds = (second); 
    } 

    public Time2(Time2 time) 
    { 
     //invoke constructor with 2 args 
     this(time.getHour(), time.getMinute(), time.getSecond()); 
    } 

    // SET and GET methods start here, also Universal time conversion and check 
    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); 
    } 

    //validate and set hour 
    public void setHour(int hour) 
    { 
     if (hour < 0 || hour >= 24) 
      throw new IllegalArgumentException("Hour must be 0-23"); 
     this.totalseconds = (hour * 3600); 
    } 

    //validate and set minute 
    public void setMinute(int minute) 
    { 
     if (minute < 0 || minute >= 59) 
      throw new IllegalArgumentException("Minute must be 0-59"); 
     this.totalseconds = (minute * 60); 
    } 

    //validate and set second 
    public void setSecond(int second) 
    { 
     if (second < 0 || second >= 24) 
      throw new IllegalArgumentException("Second must be 0-59"); 
     this.totalseconds = (second); 
    } 
    //Get Methods start here 

    //Get hour 
    public int getHour() 
    { 
     return totalseconds % 3600; 
    } 

    //get minute 
    public int getMinute() 
    { 
     return totalseconds % 60; 
    } 

    //get second 
    public int getSecond() 
    { 
     return totalseconds; 
    } 

    //convert our string to universal format (HH:MM:SS) 
    public String ToUniversalString() 
    { 
     return String.format(
     "%02d:%02d:%02d", getHour(), getMinute(), getSecond()); 
    } 

    //conver to standard format (H:MM:SS AM or PM) 
    public String toString() 
    { 
     return String.format("%d:%02d:%02d %s",((getHour() == 0 || getHour() == 
     12) ? 12 : getHour() % 12), getMinute(), getSecond(), (getHour() 
     < 12 ? "AM" : "PM")); 
    } 
}//end class Time2 



package time2; 

public class Time2Test 
{ 
    public static void main(String[] args) 
    { 
     Time2 t1 = new Time2(); //00:00:00 
     Time2 t2 = new Time2(2); //02:00:00 
     Time2 t3 = new Time2(21, 34); //21:34:00 
     Time2 t4 = new Time2(12, 25, 42); //12:25:42 
     Time2 t5 = new Time2(t4); //12:25:42 

     System.out.println("Constructed with:"); 
     displayTime("t1: all default arguments", t1); 
     displayTime("t2: hour specified; defaults for minute and second", t2); 
     displayTime("t3: hour and minute supplied second defaulted", t3); 
     displayTime("t4: hour minute and second supplied", t4); 
     displayTime("t5: Time2 object t4 specified", t5); 

     //attempt to initialize t6 with invalid args 
     try 
     { 
      Time2 t6 = new Time2(27,74,99); //all invalid values 
     } 
     catch (IllegalArgumentException e) 
     { 
      System.out.printf("%nException while initializing t6: %s%n", 
        e.getMessage()); 
     } 
    } 
    //display Time2 object in 24 hour and 12 hour formats 
    private static void displayTime(String header, Time2 t) 
    { 
     System.out.printf("%s%n %s%n %s%n", header, t.ToUniversalString(), 
       t.toString()); 
    } 
} 
+0

假设你没有将'totalseconds'指定为自午夜以来的秒数和calculatons似乎错误的总和。 –

回答

2

您反复执行以下操作:

this.totalSeconds = (hour * 3600); 
this.totalSeconds = (minute * 60); 
this.totalSeconds = second; 

这有效地将覆盖本身的三倍,所以才有了最终的价值是观察。

你在找什么是

this.totalSeconds = (hour * 3600); 
this.totalSeconds += (minute * 60); 
this.totalSeconds += second; 

否则你只是覆盖它的每一行。


此外,您使用%计算小时/分钟。例如,如果总计秒数为3672(1小时,1分钟,12秒),则这不起作用。

3672%3600 = 72.那不是多少个小时。

几小时,你想要totalSeconds/3600;分钟(totalSeconds - (3600 * getHours()))/60,以及秒钟totalSeconds - (3600 * getHours()) - (60 * getMinutes())

对不起编辑,它迟到了,我不能算算。

+0

感谢您的帮助。我明白你的意思。这不是我在编码方面遇到的问题,但非常合理。再次感谢! –

相关问题