C/< and >中布尔表达式的混淆被颠倒了?

问题描述 投票:0回答:1

我有点困惑。

< means that the right side of the number is bigger, no?

所以很自然地,while 条件

while (height > 0 && height < 9); 

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    // Ask for height:
    int height;
    do
    {
        height = get_int("Height: ");
    }

    // Has to be a number between greater than 0 and smaller than 9

    while (height > 0 && height < 9);

    int height_start = 0;
    while (height_start < height)
    {
        height_start++;
        printf("#\n");
    }

    return 0;
}

如果输入小于 9,应该只打印主题标签,对吗?

奇怪的是,它的做法完全相反,只允许我输入更大的 8 的所有内容。

c cs50 boolean-expression
1个回答
0
投票

是的,对于 (0; 9) 之间的任何高度,条件评估为 true。当它发生时,循环将继续进行另一次迭代。如果条件不再为真,即高度为 = 9 时,它会停止<= 0 or >。 希望对您有所帮助,祝您在 CS50 中玩得开心! :)

© www.soinside.com 2019 - 2024. All rights reserved.