C如果陈述不起作用

时间:2014-03-09 19:03:30

标签: c if-statement conditional

Noob在这里尝试运行一些基本代码。我有一些if语句在我尝试一起运行时没有运行,但是当我通过注释掉其他语句来隔离最后一个if语句时,它是有效的(注释在那里让你看我是怎么做的)。任何人都可以解释为什么if语句不能一起工作(如果它仍然没有用,我已经尝试了其他方法)? 此处必须分别为x,y和z选择数字1,2和3 ,输出应随机将aaa,bbb或ccc指定为1。如果代码很丑,我很抱歉。谢谢!

    int x, y, z, a, b, c, d, e, aaa, bbb, ccc, r;

    a = 0;
    b = 0;
    c = 0;
    aaa = 0;
    bbb = 0;
    ccc = 0;

    printf("Pick x:\n");
    scanf("%d",&x);

    printf("Pick y:\n");
    scanf("%d",&y);

    printf("Pick z:\n");
    scanf("%d",&z); 

    printf("You chose %d, %d, %d.\n",x,y,z);

if (x == 1 || y == 1 || z == 1) {a = (1);}

    printf("a = %d\n", a);

if (x == 2 || y == 2 || z == 2) {b = (1);}

    printf("b = %d\n", b);

if (x == 3 || y == 3 || z == 3) {c = (1);}

    printf("c = %d\n", c);

    if (a == 1 && b == 1 && c == 1 && d != 1 && e != 1) {
        /*if (aaa < bbb && aaa < ccc) {aaa = (aaa + 1);}
        if (bbb < aaa && bbb < ccc) {bbb = (bbb + 1);}
        if (ccc < bbb && ccc < aaa) {ccc = (ccc + 1);}
        if (aaa == bbb && aaa != ccc){int r = rand() % 2 + 1;
            printf("aRANDOM2 = %d\n",r);
            if (r == 1) {aaa = (aaa + 1);}
            if (r == 2) {bbb = (bbb + 1);}
        if (aaa == ccc && aaa != bbb){int r = rand() % 2 + 1;
            printf("bRANDOM2 = %d\n",r);
            if (r == 1) {aaa = (aaa + 1);}
            if (r == 2) {ccc = (ccc + 1);}
        if (ccc == bbb && aaa != ccc){int r = rand() % 2 + 1;
            printf("cRANDOM2 = %d\n",r);
            if (r == 1) {ccc = (ccc + 1);}
            if (r == 2) {bbb = (bbb + 1);}*/
        if (aaa == bbb && bbb == ccc){int r = rand() % 3 + 1;
            printf("RANDOM3 = %d\n",r);
            if (r == 1) {aaa = (aaa + 1);}
            if (r == 2) {bbb = (bbb + 1);}
            if (r == 3) {ccc = (ccc + 1);}
}}/*}}}*/


    printf("aaa = %d\n", aaa);
    printf("bbb = %d\n", bbb);
    printf("ccc = %d\n", ccc);

return 0;

1 个答案:

答案 0 :(得分:1)

在评论之前,最后三个aaa / bbb / ccc if语句中的每一个都嵌套在前一个if语句中。因此,当if (aaa == bbb && aaa != ccc)测试为false时,所有其他代码都会被跳过。

换句话说,它真正做到的是:

    if (aaa == bbb && aaa != ccc) {
        int r = rand() % 2 + 1;
        printf("aRANDOM2 = %d\n",r);
        if (r == 1) {aaa = (aaa + 1);}
        if (r == 2) {bbb = (bbb + 1);}
            if (aaa == ccc && aaa != bbb) {
            int r = rand() % 2 + 1;
            printf("bRANDOM2 = %d\n",r);
            if (r == 1) {aaa = (aaa + 1);}
            if (r == 2) {ccc = (ccc + 1);}
            if (ccc == bbb && aaa != ccc) {
                int r = rand() % 2 + 1;
                printf("cRANDOM2 = %d\n",r);
                if (r == 1) {ccc = (ccc + 1);}
                if (r == 2) {bbb = (bbb + 1);}
                if (aaa == bbb && bbb == ccc) {
                    int r = rand() % 3 + 1;
                    printf("RANDOM3 = %d\n",r);
                    if (r == 1) {aaa = (aaa + 1);}
                    if (r == 2) {bbb = (bbb + 1);}
                    if (r == 3) {ccc = (ccc + 1);}
                }
            }
        }
    }
}

你想要做的就是将三个紧密的大括号移动到各自块的末尾,如下所示:

    if (aaa == bbb && aaa != ccc) {
        int r = rand() % 2 + 1;
        printf("aRANDOM2 = %d\n",r);
        if (r == 1) {aaa = (aaa + 1);}
        if (r == 2) {bbb = (bbb + 1);}
    }
    if (aaa == ccc && aaa != bbb) {
        int r = rand() % 2 + 1;
        printf("bRANDOM2 = %d\n",r);
        if (r == 1) {aaa = (aaa + 1);}
        if (r == 2) {ccc = (ccc + 1);}
    }
    if (ccc == bbb && aaa != ccc) {
        int r = rand() % 2 + 1;
        printf("cRANDOM2 = %d\n",r);
        if (r == 1) {ccc = (ccc + 1);}
        if (r == 2) {bbb = (bbb + 1);}
    }
    if (aaa == bbb && bbb == ccc) {
        int r = rand() % 3 + 1;
        printf("RANDOM3 = %d\n",r);
        if (r == 1) {aaa = (aaa + 1);}
        if (r == 2) {bbb = (bbb + 1);}
        if (r == 3) {ccc = (ccc + 1);}
    }
}