退出随时循环 [英] Exit a loop at anytime
本文介绍了退出随时循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想退出由pressing任意键在任何时候一个循环我想要的。我试过低于code,但它无法做到的。得需要你的帮助。先谢谢你。我使用的是C-免费5.0。
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&time.h中GT;INT主要(无效)
{
INT B = 0,I;
INT秒;
的printf(秒\\ n输入数字:);
scanf函数(%d个,&安培;秒);
而(B == 0)
{
对于(i = 1; I< =秒;我++)
{
time_t的结束=时间(0)+ 1;
而(时间(0)LT端)
;
秒 - = 1;
的printf(秒数留下数:%d \\ n,秒);
B =的kbhit();
} 如果(秒== 0)
{
出口(0);
}
}
的printf(剩余秒数留下数:%d \\ n,秒);
}
解决方案
您是忙等待,在while循环最里面。这可能不是最好的解决办法,但如果这是你想要做什么,你需要在这个循环添加一个测试,以检查是否有键被击中了。
I'm trying to exit a loop at anytime I want by pressing any key. I've tried the code below but it can't be done. Gotta need your help. Thank you in advance. I'm using a C-Free 5.0.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int b=0, i;
int seconds;
printf("\nEnter number of seconds : ");
scanf("%d", &seconds);
while (b==0)
{
for(i=1;i<=seconds;i++)
{
time_t end = time(0) + 1;
while(time(0) < end)
;
seconds -= 1;
printf("Number of seconds left : %d\n", seconds);
b=kbhit();
}
if(seconds == 0)
{
exit(0);
}
}
printf("Number of remaining seconds left : %d\n", seconds);
}
解决方案
You are "busy-waiting" in the innermost while loop. That might not be the best solution, but if that is what you want to do, you need to add a test in that loop to check if a key has been hit.
这篇关于退出随时循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文