循环时的C编程.变量与变量比较



你能帮我吗?当我执行这个语句时,它总是说超出了上限,即使它不是。问题出在哪里?谢谢

if(number_o_1 <= higher)
    {
        printf("Random number has reached upper bound.n");
    }
        else
        {
            printf("Number 1: %dn", number_o_1);
            number_o_up=number_o_1+n_multiple*(x-1);
            while(number_o_up<=higher)
            for(x=2;x<number_r;x++)
            printf("Number %d: %dn", x,number_o_up);
        }
number_o_1<= higher

当数字小于或等于您的上限时,您确定要打印该数字已达到上限吗?

很难理解您想要实现的逻辑,但请参阅带有确切值和注释的示例,以访问else块。

#include <stdio.h>
int x = 2;
int higher = 100;
int number_o_1 = 10;
int number_r = 4;
int number_o_up = 1;
int n_multiple = 2;
int main()
{
    if(number_o_1 >= higher) // probably want >= here to hit else
    {
        printf("Random number has reached upper bound.n");
    }
    else
    {
        printf("Number 1: %dn", number_o_1);
        number_o_up = number_o_1 + n_multiple * (x-1); // number_o_up = 12
        while(number_o_up <= higher)                   // 12 <= 100
        {
            for(x = 2; x < number_r; x++)              // cond. 2 < 4
            {
                printf("Number %d: %dn", x, number_o_up);
            }
            number_o_up++; //needs to change higher or number_o_up to get out of while
        }
    }
}

输出:

sh-4.3$ main
Number 1: 10
Number 2: 12
Number 3: 12
Number 2: 13
Number 3: 13
Number 2: 14
...
Number 3: 100

相关内容

  • 没有找到相关文章

最新更新