现在,我正在学习 C 编程,我在“Codeproject.com”上偶然发现了一个倒计时代码,并决定运行它并分析它来学习。但是,输出 Countdown 在移动到下一个数字之前会重复每个数字数千次。请帮助我理解为什么会这样。代码如下所示:
#include <stdio.h>
#include <time.h>
int main()
{
unsigned int x_hours = 0;
unsigned int x_minutes = 0;
unsigned int x_seconds = 0;
unsigned int x_milliseconds = 0;
unsigned int totaltime = 0, count_down_time_in_secs = 0, time_left=0;
clock_t x_startTime, x_countTime;
count_down_time_in_secs = 10; // 1 min is 60
x_startTime = clock();
time_left = count_down_time_in_secs-x_seconds; //update timer
while (time_left>0)
{
x_countTime = clock();
x_milliseconds = x_countTime-x_startTime;
x_seconds=(x_milliseconds/(CLOCKS_PER_SEC))-(x_hours*60);
x_minutes=(x_milliseconds/(CLOCKS_PER_SEC))/60;
x_hours=x_minutes/60;
time_left = count_down_time_in_secs-x_seconds;
printf("\nyou have %d seconds left", time_left, count_down_time_in_secs);
}
printf("\n\n\nTime's out\n\n\n");
return 0;
}