2016-04-19 15 views
0

私はプログラミングが本当に新しいです。私はJavaへのイントロの割り当てをしています。私の割り当てでは、深夜からの総秒数を調べ、現在の時刻を表示するためにこの数を時、分、秒に変更しました。私には小さな問題があります。コードをテストすると、合計の秒数は0になります。どんな助けもありがとう。コードは無秩序です。真夜中からの総時間(秒)

 package clock; 
import java.text.DecimalFormat; 
import java.util.Calendar; // to get current time 

public class Clock { 
    public static int totalseconds; 
    public static int seconds; 
    public static int minutes; 
    public static int hours; 

    public static int test; 

    public Clock(int hours, int minutes, int seconds) { 
     setHours(hours); 
     setMinutes(minutes); 
     setSeconds(seconds); 
    } 


    // use current time 
    public Clock() { 
     Calendar c = Calendar.getInstance(); 
     long now = c.getTimeInMillis(); 
     c.set(Calendar.HOUR_OF_DAY, 0); 
     c.set(Calendar.MINUTE, 0); 
     c.set(Calendar.SECOND, 0); 
     c.set(Calendar.MILLISECOND, 0); 
     long passed = now - c.getTimeInMillis(); 
     long secondsPassed = passed/1000; 
     totalseconds = (int) secondsPassed; 


    } 


    public void tick() { 
     addSecond(); 
    } 

    private void addSecond() { 
     seconds = totalseconds%60; 
    } 

    private void addMinute() { 
     minutes = totalseconds/60 % 60; 
    } 

    private void addHour() { 
     hours = totalseconds/3600; 
    } 

    public String toString() { 
     DecimalFormat f = new DecimalFormat("00"); 
     return f.format(hours) + ":" + f.format(minutes) + ":" + f.format(seconds); 
    } 

    public int getHours() { 
     return hours; 
    } 
    public int getMinutes() { 
     return minutes; 
    } 
    public int getSeconds() { 
     return seconds; 
    } 

    // the total number of minutes sinces midnight 
    public int getTotalMinutes() { 
     return totalseconds/60 % 60; 
    } 

    // the total number of seconds since midnight 
    public int getTotalSeconds() { 
     return totalseconds; 
    } 

    public void setHours(int hours) { 
     if (hours > 23 || hours < 0) { 
      this.hours = 0; 
     } 
     else 
      this.hours = hours; 
    } 
    public void setMinutes(int minutes) { 
     if (minutes > 59 || minutes < 0) 
      this.minutes = 0; 
     else 
      this.minutes = minutes; 
    } 
    public void setSeconds(int seconds) { 
     if (seconds > 59 || seconds < 0) 
      this.seconds = 0; 
     else 
      this.seconds = seconds; 
    } 

    // reset hours, minutes and seconds to zero 
    public void reset() { 
     hours = minutes = seconds = 0; 
    } 
    public static void main(String [] args){ 

     System.out.println("this is total seconds " + test + totalseconds); 
    } 


} 
+2

あなたのクラスをインスタンス化したり、メソッドを呼び出すことはありません。なぜデフォルト値以外のものを印刷するのでしょうか? – shmosel

答えて

1

メインに変更してください。

public static void main(String [] args){ 
    Clock clock = new Clock(); 
    System.out.println("this is total seconds " + test + totalseconds); 
} 

新しいこと時計のインスタンスを作成すると、あなたのすべての魔法が発生するコンストラクタが呼び出されます。

+0

これはうまくいくかもしれませんが、静的メンバーとインスタンスメンバーのOPの混乱を無視するので、正しい解決策ではありません。 – shmosel

+0

ありがとう、私はコードの周りに行方不明の時間後に得た。ノービは私の側から間違います。 – ZML

0

@shmoselの提案に同意します。インスタンス変数の使い方には混乱があるようです。さらに、私はあなたの時間の部分のJava 8メソッドの利点を取るべきだと思います。

class Clock { 
    private int totalseconds; 
    private int seconds; 
    private int minutes; 
    private int hours; 

    int test; 

    public Clock(int hours, int minutes, int seconds) { 
     setHours(hours); 
     setMinutes(minutes); 
     setSeconds(seconds); 
    } 

    // use current time 
public Clock() { 
    LocalTime now = LocalTime.now(ZoneId.systemDefault()); 
    totalseconds = now.toSecondOfDay(); 
} 

public void tick() { 
    addSecond(); 
} 

private void addSecond() { 
    seconds = totalseconds % 60; 
} 

private void addMinute() { 
    minutes = totalseconds/60 % 60; 
} 

private void addHour() { 
    hours = totalseconds/3600; 
} 

public String toString() { 
    DecimalFormat f = new DecimalFormat("00"); 
    return f.format(hours) + ":" + f.format(minutes) + ":" + f.format(seconds); 
} 

public int getHours() { 
    return hours; 
} 

public int getMinutes() { 
    return minutes; 
} 

public int getSeconds() { 
    return seconds; 
} 

// the total number of minutes since midnight 
public int getTotalMinutes() { 
    return totalseconds/60 % 60; 
} 

// the total number of seconds since midnight 
public int getTotalSeconds() { 
    return totalseconds; 
} 

public void setHours(int hours) { 
    if (hours > 23 || hours < 0) { 
     this.hours = 0; 
    } else 
     this.hours = hours; 
} 

public void setMinutes(int minutes) { 
    if (minutes > 59 || minutes < 0) 
     this.minutes = 0; 
    else 
     this.minutes = minutes; 
} 

public void setSeconds(int seconds) { 
    if (seconds > 59 || seconds < 0) 
     this.seconds = 0; 
    else 
     this.seconds = seconds; 
} 

// reset hours, minutes and seconds to zero 
public void reset() { 
    hours = minutes = seconds = 0; 
} 
} 

public class SecondsSinceMidnight { 

    public static void main(String[] args) { 
    Clock myClock = new Clock(); 
    System.out.println("Total seconds that elapsed since midnight:" + myClock.getTotalSeconds()); 
    System.out.println("Converted to Minutes: " + myClock.getTotalMinutes()); 
    } 

} 
関連する問題