c - C 中的暂停和恢复循环

标签 c loops resume

我正在用 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/

相关文章:

c - STM32F103C8/CoIDE/ST-LinkV2 无法更新寄存器

iphone - 如何在暂停后恢复执行选择器方法?

android - 如何从 Android 中的应用程序恢复以前运行的应用程序?

c - 如何引用变量?

c - 动态函数调用

javascript - 使用循环创建新的 div,在每个新的 div 上附加子节点

mysql - foreach 循环将多行插入数据库

loops - 如何在 Brainfuck 中使用循环打印从 1 到 10 的数字?甚至有可能吗?

ios - 在 App 重新进入时恢复 UIView 动画?

c - Flex 和 Bison : Beginning a sentence with a specific keyword