编写循环时钟程序可以使用不同的编程语言来实现。以下是几种常见编程语言的示例代码:
Python 示例代码
```python
import time
def display_clock(hour, minute, second):
print(f"{hour:02d}:{minute:02d}:{second:02d}")
while True:
current_time = time.localtime()
hour = current_time.tm_hour
minute = current_time.tm_min
second = current_time.tm_sec
display_clock(hour, minute, second)
time.sleep(1)
```
C 示例代码
```c
include include void display_clock(int hour, int minute, int second) { printf("%02d:%02d:%02d\n", hour, minute, second); } int main() { time_t currentTime; while (1) { time(¤tTime); int hour = currentTime / 3600; int minute = (currentTime % 3600) / 60; int second = currentTime % 60; display_clock(hour, minute, second); sleep(1); } return 0; } ``` Java 示例代码 ```java import java.time.LocalTime; import java.time.format.DateTimeFormatter; public class Clock { public static void main(String[] args) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); while (true) { LocalTime now = LocalTime.now(); System.out.println(now.format(formatter)); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } ``` JavaScript 示例代码(适用于网页) ```html