如何循环显示每 15 分钟的时间?
how to display time every 15mins with a loop?
我的作业要求我显示用户输入的开始时间 (hh:mm:ss) 和行驶距离。出租车以 90 公里/小时的速度行驶。所以理想情况下,我希望程序显示时间(间隔 15 分钟),从开始时间到结束时间,但格式为 mm:ss。
我的做法是把开始时间和结束时间转换成秒,用for循环。我的间隔是 i+900(15 分钟秒)。但是,我的 for 循环保持 运行 并且永不结束。谁能帮帮我?
enter image description here <--- 预期输出
我当前的代码
int hour, min, sec, minhour, newmin, timesec, endtime; //ignore irrelevant variables
float distance, remdis, remdisb, firstfare, secondfare, fare;
printf("Input your starting travelling time in 24hrs clock (hh mm ss): ");
scanf("%d %d %d", &hour, &min, &sec);
printf("Input your total travelling distance (km): ");
scanf("%f", &distance);
minhour = hour * 60;
newmin = min + minhour;
printf("Your starting time is %02d:%02d:%02d or %02d:%02d (mm:ss)\n", hour, min, sec, newmin, sec);
printf("Your total travelling distance is %.1fkm or %.0fm\n", distance, distance*1000);
timesec = hour*3600 + min*60 + sec;
endtime = timesec + ((distance/90)*3600);
printf("Starting time | Fare(%.1fkm)", distance);
for (int i=timesec; i<=endtime; i+900)
{
min = i/60;
sec = i - min;
printf("%d:%d | $XX.yy\n", min, sec);
}
试试这个:
int hour, min, sec, minhour, newmin, timesec, endtime; //ignore irrelevant variables
float distance, remdis, remdisb, firstfare, secondfare, fare;
printf("Input your starting travelling time in 24hrs clock (hh mm ss): ");
scanf("%d %d %d", &hour, &min, &sec);
printf("Input your total travelling distance (km): ");
scanf("%f", &distance);
minhour = hour * 60;
newmin = min + minhour;
printf("Your starting time is %02d:%02d:%02d or %02d:%02d (mm:ss)\n", hour, min, sec, newmin, sec);
printf("Your total travelling distance is %.1fkm or %.0fm\n", distance, distance*1000);
endtime = newmin + ((distance*60)/90);
printf("Starting time | Fare(%.1fkm)\n", distance);
for (int i=newmin; i<=endtime; i+=15)
{
hour = i/60;
min = i % 60;
printf("%02d:%02d | $XX.yy\n", hour, min);
}
However, my for loop keeps running and never ends
for (int i=timesec; i<=endtime; i+900)
~~~~~~
在这里使用i+=900
,而不是i+900
。
我的作业要求我显示用户输入的开始时间 (hh:mm:ss) 和行驶距离。出租车以 90 公里/小时的速度行驶。所以理想情况下,我希望程序显示时间(间隔 15 分钟),从开始时间到结束时间,但格式为 mm:ss。
我的做法是把开始时间和结束时间转换成秒,用for循环。我的间隔是 i+900(15 分钟秒)。但是,我的 for 循环保持 运行 并且永不结束。谁能帮帮我?
enter image description here <--- 预期输出
我当前的代码
int hour, min, sec, minhour, newmin, timesec, endtime; //ignore irrelevant variables
float distance, remdis, remdisb, firstfare, secondfare, fare;
printf("Input your starting travelling time in 24hrs clock (hh mm ss): ");
scanf("%d %d %d", &hour, &min, &sec);
printf("Input your total travelling distance (km): ");
scanf("%f", &distance);
minhour = hour * 60;
newmin = min + minhour;
printf("Your starting time is %02d:%02d:%02d or %02d:%02d (mm:ss)\n", hour, min, sec, newmin, sec);
printf("Your total travelling distance is %.1fkm or %.0fm\n", distance, distance*1000);
timesec = hour*3600 + min*60 + sec;
endtime = timesec + ((distance/90)*3600);
printf("Starting time | Fare(%.1fkm)", distance);
for (int i=timesec; i<=endtime; i+900)
{
min = i/60;
sec = i - min;
printf("%d:%d | $XX.yy\n", min, sec);
}
试试这个:
int hour, min, sec, minhour, newmin, timesec, endtime; //ignore irrelevant variables
float distance, remdis, remdisb, firstfare, secondfare, fare;
printf("Input your starting travelling time in 24hrs clock (hh mm ss): ");
scanf("%d %d %d", &hour, &min, &sec);
printf("Input your total travelling distance (km): ");
scanf("%f", &distance);
minhour = hour * 60;
newmin = min + minhour;
printf("Your starting time is %02d:%02d:%02d or %02d:%02d (mm:ss)\n", hour, min, sec, newmin, sec);
printf("Your total travelling distance is %.1fkm or %.0fm\n", distance, distance*1000);
endtime = newmin + ((distance*60)/90);
printf("Starting time | Fare(%.1fkm)\n", distance);
for (int i=newmin; i<=endtime; i+=15)
{
hour = i/60;
min = i % 60;
printf("%02d:%02d | $XX.yy\n", hour, min);
}
However, my for loop keeps running and never ends
for (int i=timesec; i<=endtime; i+900) ~~~~~~
在这里使用i+=900
,而不是i+900
。