0

嗨,我试图理解为什么如果输入 9 或以上来判断它通过但它不应该导致 if 说 >= 4 和 <= 8

谢谢

while(!(judge >= 4) && (judge <= 8))
{
    printf("How many judges are there ? Enter a number between 4 - 8 \n");
    scanf("%d", &judge);
    while(!(judge >= 4) && (judge <= 8))
    {
        printf("You entered %d Enter a number between 4 - 8 \n", judge);
        scanf("%d", &judge);
        if((judge >= 4) && (judge <= 8))
        {
            break;
        }
    }

}
4

1 回答 1

1

看起来你缺少一对括号

while(!((judge >= 4) && (judge <= 8)))
       ^                            ^

(这个错误出现在两个地方。)

顺便说一句,您可以通过像这样重组代码来避免大量重复:

printf("How many judges are there ? Enter a number between 4 - 8 \n");
for (;;) {
    scanf("%d", &judge);
    if (judge >= 4 && judge <= 8) {
        break;
    }
    printf("You entered %d Enter a number between 4 - 8 \n", judge);
}
于 2013-03-25T06:49:21.523 回答