我的时钟代码每隔一小时工作一次,除了10点钟。时钟。在每隔一个小时,每当秒为60时,它会将分钟增加1,但是在10分钟时,它会增加1分钟。时钟,出于某种原因,每当秒数为10时,它会将分钟增加1.我不知道我做错了什么。请帮忙!
package misk;
public class Misk {
public static void main(String[] args) throws InterruptedException {
int x = 0;
int sec = 0, min = 0, hour = 9;
while (x == 0) {
Thread.sleep(10);
sec++;
if (sec == 60) {
sec = 0;
min++;
}
if (min == 60) {
min = 0;
hour++;
}
if (sec < 10) {
if (min < 10) {
if (hour < 10) {
System.out.println("0" + hour + ":0" + min + ":0" + sec);
}
}
}
if (sec > 10) {
if (min < 10) {
if (hour < 10) {
System.out.println("0" + hour + ":0" + min + ":" + sec);
}
}
}
if (sec < 10) {
if (min > 10) {
if (hour < 10) {
System.out.println("0" + hour + ":" + min + ":0" + sec);
}
}
}
if (sec < 10) {
if (min < 10) {
if (hour > 10) {
System.out.println("" + hour + ":0" + min + ":0" + sec);
}
}
}
if (sec > 10) {
if (min > 10) {
if (hour < 10) {
System.out.println("0" + hour + ":" + min + ":" + sec);
}
}
}
if (sec < 10) {
if (min > 10) {
if (hour > 10) {
System.out.println("0" + hour + ":" + min + ":" + sec);
}
}
}
if (sec > 10) {
if (min < 10) {
if (hour > 10) {
System.out.println("" + hour + ":0" + min + ":" + sec);
}
}
}
if (sec > 10) {
if (min > 10) {
if (hour > 10) {
System.out.println("" + hour + ":" + min + ":" + sec);
}
}
}
if (sec == 10) {
if (min == 10) {
if (hour == 10) {
System.out.println("" + hour + ":" + min + ":" + sec);
}
}
}
if (sec > 10) {
if (min == 10) {
if (hour == 10) {
System.out.println("" + hour + ":" + min + ":" + sec);
}
}
}
if (sec == 10) {
if (min > 10) {
if (hour == 10) {
System.out.println("" + hour + ":" + min + ":" + sec);
}
}
}
if (sec == 10) {
if (min == 10) {
if (hour > 10) {
System.out.println("" + hour + ":" + min + ":" + sec);
}
}
}
if (sec > 10) {
if (min > 10) {
if (hour == 10) {
System.out.println("" + hour + ":" + min + ":" + sec);
}
}
}
if (sec == 10) {
if (min > 10) {
if (hour > 10) {
System.out.println("" + hour + ":" + min + ":" + sec);
}
}
}
if (sec > 10) {
if (min == 10) {
if (hour > 10) {
System.out.println("" + hour + ":" + min + ":" + sec);
}
}
}
if (sec < 10) {
if (min == 10) {
if (hour == 10) {
System.out.println("" + hour + ":" + min + ":" + sec);
}
}
}
if (sec == 10) {
if (min < 10) {
if (hour == 10) {
System.out.println("" + hour + ":0" + min + ":" + sec);
}
}
}
if (sec == 10) {
if (min == 10) {
if (hour < 10) {
System.out.println("" + hour + ":" + min + ":" + sec);
}
}
}
if (sec < 10) {
if (min < 10) {
if (hour == 10) {
System.out.println("" + hour + ":0" + min + ":0" + sec);
}
}
}
if (sec == 10) {
if (min < 10) {
if (hour < 10) {
System.out.println("0" + hour + ":0" + min + ":" + sec);
}
}
}
if (sec < 10) {
if (min == 10) {
if (hour < 10) {
System.out.println("0" + hour + ":" + min + ":0" + sec);
}
}
}
if (sec == 10) {
if (min < 10) {
if (hour > 10) {
System.out.println("" + hour + ":0" + min + ":" + sec);
}
}
}
if (sec < 10) {
if (min == 10) {
if (hour > 10) {
System.out.println("" + hour + ":" + min + ":0" + sec);
}
}
}
if (sec > 10) {
if (min == 10) {
if (hour < 10) {
System.out.println("0" + hour + ":" + min + ":" + sec);
}
}
}
if (sec < 10) {
if (min == 10) {
if (hour < 10) {
System.out.println("0" + hour + ":" + min + ":0" + sec);
}
}
}
if (sec == 10) {
if (min > 10) {
if (hour < 10) {
System.out.println("0" + hour + ":" + min + ":" + sec);
}
}
}
}
}
}
答案 0 :(得分:3)
首先,你的代码很乱。
您需要学习如何更有效地使用条件,还要了解格式化。
您可以轻松添加&#39; 0&#39;在任何数字之前,如果它不是一个&#39; 2位数字&#39;。
System.out.println(String.format("%02d %02d %02d", hour, minute, second));
你的代码在10点钟没有工作。时钟,因为你所有的条件都是&amp;小时&gt; 10&#39;或者&#39;小时&lt; 10&#39;,hour == 10
时,这两个都是假的。