写一个周期报时的C#代码
时间: 2023-10-19 10:18:38 浏览: 127
循环报时可以使用定时器和if语句来实现,代码如下:
```c
#include <stdio.h>
#include <time.h>
#include <windows.h>
int main()
{
int hour, minute, second;
while (1) {
// 获取当前时间
time_t now = time(NULL);
struct tm *t = localtime(&now);
hour = t->tm_hour;
minute = t->tm_min;
second = t->tm_sec;
// 输出当前时间
printf("%02d:%02d:%02d ", hour, minute, second);
// 判断是否到整点
if (minute == 0 && second == 0) {
for (int i = 0; i < hour; i++) {
printf("Dong ");
}
}
// 定时器延时1秒
Sleep(1000);
}
return 0;
}
```
该程序会每秒输出当前时间,并在整点报时时输出对应的“Dong”声,例如:
```
11:59:50
11:59:51
11:59:52
11:59:53
11:59:54
11:59:55
11:59:56
11:59:57
11:59:58
11:59:59
12:00:00 Dong
12:01:00 Dong Dong
12:02:00 Dong Dong Dong
12:03:00 Dong Dong Dong Dong
12:04:00 Dong Dong Dong Dong Dong
12:05:00 Dong Dong Dong Dong Dong Dong
...
```
阅读全文