-2

我是 C++ 的初学者,我正在尝试对 ifs 中的 ifs 进行练习。

第一张图片:http: //img51.imageshack.us/img51/5851/b9qt.jpg 没有 else if 语句所以它被发布了两次,但为什么它有错误的逻辑?不过,发布的第二个声明是正确的。o_o

第二张图片:http: //img96.imageshack.us/img96/1351/94bc.jpg 这次用了else if语句,贴过一次,但逻辑错误。我一遍又一遍地分析代码,我找不到任何问题。XD 使用的逻辑不应该属于最后一条语句吗?o_o

int number1, number2, number3, number4, number5;

printf("\n\n First number: ");
scanf("%d", &number1);
printf("\n Second number: ");
scanf("%d", &number2);
printf("\n Third number: ");
scanf("%d", &number3);
printf("\n Fourth number: ");
scanf("%d", &number4);
printf("\n Fifth number: ");
scanf("%d", &number5);

if (number3 >= number4 && number5) {
    if (number4 >= number5) {
    printf("\n\n Your numbers have been sorted: ");
    printf("%d, %d, %d, %d, %d.", number1, number2, number3, number4, number5);

    printf("\n\n\n Program will now terminate..");
    } else if (number5 >= number4) {
    printf("\n\n Your numbers have been sorted: ");
    printf("%d, %d, %d, %d, %d.", number1, number2, number3, number5, number4);

    printf("\n\n\n Program will now terminate..");
    } else {
    printf("\n\n Invalid operation. Program will now terminate..");
    }
} else if (number4 >= number3 && number5) {
    if (number3 >= number5) {
    printf("\n\n Your numbers have been sorted: ");
    printf("%d, %d, %d, %d, %d.", number1, number2, number4, number3, number5);

    printf("\n\n\n Program will now terminate..");
    } else if (number5 >= number3) {
    printf("\n\n Your numbers have been sorted: ");
    printf("%d, %d, %d, %d, %d.", number1, number2, number4, number5, number3);

    printf("\n\n\n Program will now terminate..");
    } else {
    printf("\n\n Invalid operation. Program will now terminate..");
    }
} else if (number5 >= number3 && number4) {
    if (number3 >= number4) {
    printf("\n\n Your numbers have been sorted: ");
    printf("%d, %d, %d, %d, %d.", number1, number2, number5, number3, number4);

    printf("\n\n\n Program will now terminate..");
    } else if (number4 >= number3) {
    printf("\n\n Your numbers have been sorted: ");
    printf("%d, %d, %d, %d, %d.", number1, number2, number5, number4, number3);

    printf("\n\n\n Program will now terminate..");
    } else {
    printf("\n\n Invalid operation. Program will now terminate..");
    }
} else {
    printf("\n\n Invalid operation. Program will now terminate..");
}
4

2 回答 2

3

这是解决此问题的一种非常糟糕的方法,但是随着您的不断学习,您稍后会理解为什么。现在,作为理解 if 语句的练习,一个问题是

if(number3 >= number4 && number5) {
    ...
}

不做你认为它做的事。它实际上被解释得更像

if( (number3 >= number4) && (number5) )

请注意,number5单独在那里。C++ 可以让你说类似的东西

bool someFlag = true;
if(someFlag) { ... }

作为简写

if(someFlag == true)

结合这一点,在 C++(和 C)中,任何数字都可以被视为布尔值。那是,

int x = 42;
int y = 0;
if(x) {
    printf("x is true\n");
}
if(y) {
    printf("y is true\n");
}

此代码将打印“x 为真”,但不会打印“y 为真”,因为 0 在解释为布尔值时被认为是假的,而任何其他数字都为真。

所以回到你原来的代码,你有

if(number3 <= number4 && number5)

这是两个不同的布尔比较,如果两个部分都为真,则 if 语句为真。第 1 部分是(number3 <= number4)您已经了解的。第 2 部分是 just number5,它遵循我刚才描述的相同规则。

你需要说的是

if(number3 <= number4 && number3 <= number5)

没有简短的符号。

于 2013-10-26T19:24:19.123 回答
0

if语句中的逻辑操作有问题。语法是正确的,但结果不是您所期望的。此语句检查 ifnumber3是否大于等于number4并且为 1 if true。然后这个结果将应用于下一个逻辑操作。Saynumber3 >= number4为真且number5>0,则整个表达式为true

number3 >= number4 && number5

因此,这不会检查number3is greater equalnumber4和 greater equal number5。相反,你必须写

(number3 >= number4) && (number3 >= number5)

and is trueifnumber3确实更大 等于number4and number5

于 2013-10-26T19:32:16.533 回答