2017-08-17 59 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

感謝您的幫助。我明白你的意思。這不是我在編碼方面遇到的問題,但非常合理。再次感謝! –

相關問題