我知道我可以用更少的行來撰寫這段代碼,而且我以前也這樣做過,但是我嘗試使用“if else”鏈來探索撰寫同一程式的不同方法,因為我仍在學習基礎知識。但是,如果第三個條件(C 是更大的條件)為真,那么這樣做我無法得到結果。只有當第三個條件為真時,程式才結束而不列印任何東西。我究竟做錯了什么?
#include <stdio.h>
#include <conio.h>
int main(){
int a, b, c = 0;
printf("Please insert three different numbers:\n");
printf("Insert A value:");
scanf("%d", &a);
printf("Insert B value:");
scanf("%d", &b);
printf("Insert C value:");
scanf("%d", &c);
//comparing A, B and C to find the greater number
if(a > b){
if (a > c){
printf("\nThe greater is A: %d", a);
}
}else if (b > a){
if(b > c){
printf("\nThe greater is B: %d", b);
}
}else{
printf("\nThe greater is C: %d", c);
}
getch();
return 0;
}
uj5u.com熱心網友回復:
將您if
的陳述更改為:
if(a > b && a > c){
printf("\nThe greater is A: %d", a);
}else if (b > a && b > c){
printf("\nThe greater is B: %d", b);
}else if (c > a && c > b){
printf("\nThe greater is C: %d", c);
}
此外,使用您擁有的相同結構,您可以使用它:
if(a > b){
if (a > c){
printf("\nThe greater is A: %d", a);
}else {
printf("\nThe greater is C: %d", c);
}
}else if (b > a){
if(b > c){
printf("\nThe greater is B: %d", b);
}else{
printf("\nThe greater is C: %d", c);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/529467.html
上一篇:檢查條件Python