意外的结果是

时间:2013-03-25 06:47:49

标签: c testing

嗨我试图理解为什么如果输入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;
        }
    }

}

1 个答案:

答案 0 :(得分: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);
}