我正在用 C 语言编写一个游戏代码,其中涉及显示数字以及用户输入的时间间隔。
srand ( time(NULL) ); //this avoids generating the same random number everytime
start:
mySleep(userdelay);
printf("\n\n\a\t%d",rand()%90); //prints random numbers with the time delay entered by the user.
goto start;
所以我只是想知道如何通过用户输入(如“按 1 暂停”)暂停此循环,并如何通过用户输入(如“按 2 恢复”)恢复相同的循环。
提前致谢!
最佳答案
我不确定这可能是最好的方法,但它可能是一种可行的方法。尝试在命令控制台中使用 while 循环:
int main(){
int i;
scanf("%d",&i)
switch(i){
case 1: while(i != 2){
sleep(1); //or define duration by a variable if time should be "dynamic"
scanf("%d",&i);
break;
}
}
}
我希望您能了解这个想法。
编辑:
如果您希望用户输入数据直到“退出键”,那么 while 循环比 goto 好得多。如果您不熟悉 switch(){}
我建议您看一下 here 。
关于c - C 中的暂停和恢复循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11015348/