Attempting to make a clock in java in my spare time, having lots of fun and two hours in i am still getting problems with my code.
it tells me the current time but advances way too quickly.
(copy and paste it into an IDE/Compiler and run it and you will see)
Can anyone help a beginner?
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);
}
}