Closed. This question is off-topic. It is not currently accepting answers. Learn more
想改进这个问题吗?Update the question所以堆栈溢出的值小于aa>。
三年前关闭。
我试图提示用户输入一个不能超过23的正整数,如果不满足这个条件,用户应该被重新提示。到目前为止这是我的代码:
int main(void) {
    int n = 0;
    do
    {
        printf("please give me a positive integer not greater than 23:");
        n = GetInt();
    }
    while (n<0);

    printf ("Thanks for the Valid Number!");
    return 0;
}

('GetInt()'是我正在使用的库函数)。
然而,对于while条件,当我添加&& n>23以满足第二个条件时,我的代码将失败,对于负数和大于23的数,代码都将失败。是不是我遗漏了什么。
注:我对C是完全陌生的,所以我自己也在努力学习,如果有人能帮助我,那就太好了。

最佳答案

int n;

do {
    printf("Enter a number: ");
    scanf("%d", &n);
} while(n<0 || n>23);

这基本上就是代码。
你想要的是输入一个数字。
如果数字小于0或大于23,则条件将重新启动一个输入,从而产生一个while循环。

关于c - C中针对相同整数的多个条件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35503026/

10-11 22:13
查看更多