我正在制作24小时制

时间:2013-11-29 03:07:43

标签: java arrays clock

我正在做24小时。我有下面的代码。我的问题是int[]时间等于[0, 0, 0]并在每次迭代时打印出来。
一些可能对您有帮助的信息。我的Array TimeCounter[hours, minutes, seconds]。 reset方法将选择的int值设置为零,保持不变或增加1。嵌套的while循环用于通过时钟。

public static int hours;
public static int minutes;
public static int seconds;
public static int [] TimeCounter = {hours, minutes, seconds};

//End of day
public static void reset(int [] midNight){
    hours = 0;
    minutes = 0;
    seconds = 0;
}

//End of a minute
public static void resetSeconds(int [] endOfMin){
    minutes++;
    seconds = 0;
}

//End of a Hour
public static void resetMinutes(int [] endOfHour){
    hours++;
    minutes = 0;
    seconds = 0;
}
public static void run(int [] time){
    hours = 0;
    minutes = 0;
    seconds = 0;
    System.out.println(Arrays.toString(time));
    while (hours < 25){
        if (hours == 24){
            if (minutes == 59){
                if (seconds == 60){
                    reset(time);
                }
            }
        }
        while (minutes < 61){
            if (minutes == 59){
                if (seconds == 60){
                    resetMinutes(time);
                    System.out.println(Arrays.toString(time));
                }
            }
            while (seconds < 61){
                if (seconds == 60){
                    resetSeconds(time);
                }
                System.out.println(Arrays.toString(time));
                seconds++;

            }
            System.out.println(Arrays.toString(time));
            minutes++;
        }
        System.out.println(Arrays.toString(time));
        hours++;
    }

}

public static void main(String[] args){

    run(TimeCounter);
}

2 个答案:

答案 0 :(得分:5)

数组所包含的整数与变量中的整数不同。是的,您使用变量初始化数组,但在此之后,数组int项目是独立的。如果要在数组中增加int项,则必须直接执行:myArray[someIndex]++

顺便说一句,根据我的评论,你是否不允许在这个程序中使用面向对象的概念?这些说明是否强迫您使用静态做什么?我问这个的原因是因为一个更好的程序不会像你一样使用数组,但会有几秒钟,几分钟和几小时的int字段,并且会将逻辑封装在几秒到几分钟到几小时之间。这将很多更容易维护和增强。


修改
例如:

public class FooClock {
   public static final int SECONDS_PER_MINUTE = 60;
   public static final int MINUTES_PER_HOUR = 60;
   private static final int NOON = 12;
   private int hours;
   private int minutes;
   private int seconds;
   private boolean am = true;
   private String formatString = "%02d:%02d:%02d %s";

   public FooClock(int hours, int minutes, int seconds, boolean am) {
      this.hours = hours;
      this.minutes = minutes;
      this.seconds = seconds;
      this.am = am;
   }

   public void incrementSeconds() {
      seconds++;
      normalize();
   }

   public void incrementMinutes() {
      minutes++;
      normalize();
   }

   public void incrementHours() {
      hours++;
   }

   public void normalize() {
      if (seconds >= SECONDS_PER_MINUTE) {
         minutes += seconds / SECONDS_PER_MINUTE;
         seconds %= SECONDS_PER_MINUTE;
      }
      if (minutes >= MINUTES_PER_HOUR) {
         hours += minutes / MINUTES_PER_HOUR;
         minutes %= MINUTES_PER_HOUR;
      }
      if (hours >= 2 * NOON) {
         hours %= (2 * NOON);
         am = true;
      }
      if (hours >= NOON) {
         am = false;
      }
   }

   @Override
   public String toString() {
      String amString = am ? "AM" : "PM";
      return String.format(formatString, hours, minutes, seconds, amString);
   }

   public static void main(String[] args) {
      int total_seconds = 24 * SECONDS_PER_MINUTE * MINUTES_PER_HOUR;

      FooClock fooClock = new FooClock(0, 0, 0, true);
      System.out.println(fooClock);
      for (int i = 0; i < total_seconds; i++) {
         fooClock.incrementSeconds();
         System.out.println(fooClock + " ");
      }
   }
}

答案 1 :(得分:0)

我所做的是使用几乎完全相同的代码将while循环更改为for循环。我还添加了诸如“am”和“pm”之类的东西

public static int hours;

public static int minutes;

public static int seconds;

public static int [] TimeCounter = {hours, minutes, seconds};

public static void run(int h, int m, int s){
    hours = h;
    minutes = m;
    seconds = s;
    TimeCounter[0] = hours;
    TimeCounter[1] = minutes;
    TimeCounter[2] = seconds;
    String timeOfDay = "am";

    for (hours = 0; hours < 25; hours++){
        TimeCounter[0] = hours;
        if (hours == 12){
            timeOfDay = "pm";
        }
        System.out.println(TimeCounter[0] + " : " + TimeCounter[1] + " : " + TimeCounter[2] + " " + timeOfDay);
        if (hours == 24){
            reset(TimeCounter);
            break;
        }else{
            for (int minutes = 0; minutes < 61; minutes++){
                TimeCounter[1] = minutes;
                System.out.println(TimeCounter[0] + " : " + TimeCounter[1] + " : " + TimeCounter[2] + " " + timeOfDay);
                }
                if (minutes == 60){
                    minutes = 0;
                    break;
                }else{
                    for (seconds = 0; seconds < 61; seconds++){
                        TimeCounter[2] = seconds;
                        System.out.println(TimeCounter[0] + " : " + TimeCounter[1] + " : " + TimeCounter[2] + " " + timeOfDay);
                        if (seconds == 60){
                            seconds = 0;
                            break;
                        }
                    }
                }
            }
        }

}

//End of day
public static void reset(int [] midNight){
    TimeCounter[0] = 0;
    TimeCounter[1] = 0;
    TimeCounter[2] = 0;
}

public static void main(String[] args){
    run(0, 0, 1);
}

}