public class Solution {
public static volatile boolean isStopped = false;
public static void main(String[] args) throws InterruptedException {
Clock clock = new Clock("Лондон", 23, 59, 57);
Thread.sleep(4000);
isStopped = true;
Thread.sleep(1000);
}
public static class Clock extends Thread {
private String cityName;
private int hours;
private int minutes;
private int seconds;
public Clock(String cityName, int hours, int minutes, int seconds) {
this.cityName = cityName;
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
start();
}
public void run() {
try {
while (!isStopped) {
printTime();
}
} catch (InterruptedException e) {
}
}
private void printTime() throws InterruptedException {
Thread.sleep(1000);
seconds=++seconds;
if(seconds>59){seconds=0;minutes++;};
if(minutes>59){minutes=0;hours++;}
if(hours>23){hours=0;}
if (hours == 0 && minutes == 0 && seconds == 0) {
System.out.println(String.format("В г. %s сейчас полночь!", cityName));
} else {
System.out.println(String.format("В г. %s сейчас %d:%d:%d!", cityName, hours, minutes, seconds));
}
}
}
}
В момент когда четыре записи выведены на экран - меняется переменная public static volatile boolean isStopped = false;, и программа останавливается. ПОЧЕМУ именно в этот момент она меняется - ведь алгоритм смены не прописан. спасибо
Дмитрий
24 уровень
задачу решил норм, но не понимаю один момент
Обсуждается
Комментарии (3)
- популярные
- новые
- старые
Для того, чтобы оставить комментарий Вы должны авторизоваться
hidden #2322530
15 сентября 2020, 16:05
в методе main все прописано
+1
Дмитрий
17 сентября 2020, 18:11
да, но почему она меняется имеенно когда результат = -1. а не скажем -49, или 0
0
hidden #2322530
17 сентября 2020, 18:13
какой результат равен -1 ? откуда ты его взял вообще
0