我是 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..");
}