'Java Booleans not updating
First of all, sorry if I mess up, it's my first question here. Second, sorry for my messy code, I just started learning java at school.
So, I coded a timer that goes up from 00:00:00 to 23:59:59. The seconds, minutes, and hours go up using inner and outer for loops and are then printed every second in the console. I got a boolean that checks if the values are less than 10. If the number is less than 10, it adds a 0 before the number so that it's printed as 01:02:03 instead of 1:2:3. The problem is that the program doesn't update the value of the booleans once the values are equal or greater than 10, printing 010:011:012 instead of 10:11:12. Any ideas?
public class Clock {
int seconds = 0;
int minutes = 0;
int hours = 0;
boolean hoursLess10;
boolean minutesLess10;
boolean secondsLess10;
String secondDisplay;
String minuteDisplay;
String hourDisplay;
String timeDisplay;
public static void main(String[] args) {
Clock m = new Clock();
m.startClock();
}
public void startClock() {
try {
for (int hourElapse = 0; hourElapse < 24; hourElapse++) {
if (hours < 10) {
hoursLess10 = true;
} else {
hoursLess10 = false;
}
for (int minuteElapse = 0; minuteElapse < 60; minuteElapse++) {
if (minutes < 10) {
minutesLess10 = true;
} else {
minutesLess10 = false;
}
for (int secondElapse = 0; secondElapse < 60; secondElapse++) {
if (seconds < 10) {
secondsLess10 = true;
} else if (seconds >= 10) {
secondsLess10 = false;
}
displayTime();
Thread.sleep(1000);
seconds++;
}
seconds = 0;
minutes++;
}
minutes = 0;
hours ++;
}
hours = 0;
} catch (Exception e) {
System.out.println("error");
}
System.out.println(hoursLess10);
System.out.println(minutesLess10);
System.out.println(secondsLess10);
}
public boolean displaySeconds() {
if (secondsLess10 = true) {
/*secondDisplay = (String) "0" + seconds;*/
System.out.println("0" + seconds);
} else {
/*secondDisplay = (String) "" + seconds;*/
System.out.print(seconds);
}
return secondsLess10;
}
public boolean displayMinutes() {
if (minutesLess10 = true) {
/*minuteDisplay = (String) "0" + minutes;*/
System.out.print("0" + minutes);
System.out.print(":");
} else {
/*minuteDisplay = (String) "" + minutes;*/
System.out.print(minutes);
System.out.print(":");
}
return minutesLess10;
}
public boolean displayHours() {
if (hoursLess10 = true) {
/*hourDisplay = (String) "0" + hours;*/
System.out.print("0" + hours);
System.out.print(":");
} else {
/*hourDisplay = (String) "" + hours;*/
System.out.print(hours);
System.out.print(":");
}
return hoursLess10;
}
public void displayTime() {
displayHours();
displayMinutes();
displaySeconds();
}
}
Solution 1:[1]
In displayHours, displayMinutes and displaySeconds methods, there are mistakes in if condition.
When you want to check the boolean variable, you can do it in 2 ways.
// == ... check if there are the same
// 1) != ... check if there are not the same
if (minutesLess == 10){
//To Do Things
}
// 2) if the boolean variable is true, for false it should be !minutesLess
if (minutesLess){
//To Do Things
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | schlebe |
