C编程while循环。与变量

时间:2016-04-14 10:03:56

标签: c variables while-loop compare

你能帮帮我吗?当我执行这个语句时,即使它不是,它也会一直说出上限。问题是什么?感谢。

if(number_o_1 <= higher)
    {
        printf("Random number has reached upper bound.\n");

    }
        else
        {
            printf("Number 1: %d\n", 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: %d\n", x,number_o_up);
        }

2 个答案:

答案 0 :(得分:0)

number_o_1<= higher

当你的数字小于或等于你的上限时,你是否想要打印数字已达到上限?

答案 1 :(得分:0)

要想实现您想要实现的逻辑有点困难,但请查看带有确切值和注释的示例以触及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: %d\n", 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: %d\n", 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