c - 再次自动运行 C 代码

标签 c

如果最初输入的值小于 0 或大于 23,我希望用户能够立即重新输入另一个值。目前,我的代码输出“请重新输入小于 24 的值”并停止。然后必须再次运行它,然后用户才能重新输入另一个值。

{
    printf ("What is the height of the half-pyramids?\n");
        int height = get_int();
        if (height>23)
        {
            printf("please re-enter a value less than 24\n");
        }
        else if (height<0)
        {
            printf("please re-enter a value more than 0\n");
        }
        else
        {
            printf("%i", height);
    }

}

最佳答案

使用循环。

while(1) {
    printf ("What is the height of the half-pyramids?\n");
    int height = get_int();
    if (height>23)
    {
        printf("please re-enter a value less than 24\n");
    }
    else if (height<0)
    {
        printf("please re-enter a value more than 0\n");
    }
    else
    {
        printf("%d\n", height);
        break; // stop stop looping
}

顺便说一句,使用 %d 打印整数更为惯用,而不是 %i。请参阅What is the difference between conversion specifiers %i and %d in formatted IO functions (*printf / *scanf)

关于c - 再次自动运行 C 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46349935/

相关文章:

python - 使用 Python 对 C 代码进行单元测试的最简单方法

c 管道读取字符串为空

objective-c - 在 C 中访问 Apple Earbud Clicker 控件

c - 如何使用strtok分隔和获取新的字符串

c++ - 我可以在 C 或 C++ 中使用二进制文字吗?

c - fgets什么时候开始读取?

无法在 C 中打印出字符串

c - 使用 GLib 中的 GHashTable?

c - 如何在 GAS 中设置 .global 符号?

创建嵌套 pthread