需求
代码
public class Test16 {标签:10,java,String,hour,int,230222,second,minute,时钟 From: https://blog.51cto.com/u_13137233/6078846
public static void main(String[] args) throws InterruptedException {
// 电子时钟
// 小时循环
for (int hour = 0; hour < 24; hour++) {
// 分钟循环
for (int minute = 0; minute < 60; minute++) {
// 秒钟循环
for (int second = 0; second < 60; second++) {
Thread.sleep(1000); // alt enter...add exception...
// 输出当前的时间
// 小时:分钟:秒钟
// 小于10的前面加0
String newHour = hour < 10 ? "0" + hour : "" + hour;
String newMinute = minute < 10 ? "0" + minute : "" + minute;
String newSecond = second < 10 ? "0" + second : "" + second;
String msg = String.format("%s:%s:%s", newHour, newMinute, newSecond);
System.out.println(msg);
}
}
}
}
}