Reputation: 73
I have just started learning java and seeking help how to properly implement nested loops to get it working correctly.
Increment this Clock by 1 second. Minutes and hours updated as needed
public void increment () {
if (seconds < 59)
seconds = seconds + 1;
else
seconds = 0;
if (seconds <= 0)
if (minutes < 59)
minutes = minutes + 1;
else
minutes = minutes;
if (minutes <= 0)
if (hours < 23)
hours = hours + 1;
else
hours = 0;
This code doesn't work how it intend to be. Thanks in Advance
Upvotes: 0
Views: 120
Reputation: 37691
I guess you are looking for something like this.
public static void main(String[] args) {
increment(19, 43, 59);
increment(23, 59, 59);
increment(7, 19, 45);
}
public static void increment(int hours, int minutes, int seconds) {
if (seconds < 59) {
seconds = seconds + 1;
} else {
seconds = 0;
if (minutes < 59) {
minutes = minutes + 1;
} else {
minutes = 0;
if (hours < 23) {
hours = hours + 1;
} else {
hours = 0;
}
}
}
System.out.println(hours + ":" + minutes + ":" + seconds);
}
Output:
19:44:0
0:0:0
7:19:46
Please note, this is just an example which you can exploit as per your need.
Upvotes: 1