試圖在業餘時間用java製作一個時鐘,有很多樂趣和兩個小時,但我仍然遇到了我的代碼問題。Java時鐘太快
它告訴我當前時間,但進展太快。
(複製並粘貼到一個IDE /編譯並運行它,你會看到)
誰能幫助初學者?
package clock;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
public class Clock {
public static void main(String[] args) {
int hour;
int minute;
int second;
Scanner keyb = new Scanner(System.in);
System.out.println("What hour is it?");
hour = keyb.nextInt();
System.out.println("What Minute is it?");
minute = keyb.nextInt();
second = 0;
Timer t = new Timer();
t.schedule(new TimerTask() {
Scanner keyb = new Scanner(System.in);
int hour;
int minute;
int second;
int turnOn;
@Override
public void run() {
turnOn = 1;
while (turnOn != 0){
System.out.println("Current time is:"+ hour +":"+minute+":"+second);
second ++;
if (second >= 59){
minute ++;
second = 0;
}
else{
}
if (minute >= 59){
hour ++;
minute = 0;
}
else{
}
if (hour >= 24){
hour = 0;
}
}
}
}, 0, 1000);
}
}
將停止while循環一直持續到下一秒爲什麼不使用'turnOn'一個'boolean'後改變所述開啓INT爲0解決了這個? – JonK
嗨好主意,只是改變了! –