新手在尝试运行一些基本代码。我有一些 if 语句,当我尝试一起运行它们时,它们就不起作用了,但是当我通过注释其他 if 语句来隔离最后一个 if 语句时,它就可以工作(为您展示了如何做到这一点的注释)。有人能解释一下为什么这些 if 语句无法一起工作吗?(我尝试了 else if,但还是不行)。
在这里,您必须将数字 1、2 和 3 分别选择为 x、y 和 z,输出应随机将 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;
d
和e
被使用了,但没有被初始化。 - pat