代碼縮進混亂、大括號錯位,導致 `if-else` 匹配錯誤,`printf` 執行流程不符預期。
int main() {
char sex, sports, diet;
double faHeight, moHeight, myHeight;
// ... 輸入 ...
if (sex == 'F') {
double h1 = (sports=='Y') ? ((faHeight*0.54+moHeight)/2*0.02) : 0;
double h2 = (diet=='Y') ? ((faHeight*0.54+moHeight)/2*0.015) : 0;
myHeight = (faHeight+moHeight)*0.54 + h1 + h2;
} else {
double h1 = (sports=='Y') ? ((faHeight+moHeight)*0.54*0.02) : 0;
double h2 = (diet=='Y') ? ((faHeight+moHeight)*0.54*0.015) : 0;
myHeight = (faHeight*0.923+moHeight)/2 + h1 + h2;
}
printf("%f", myHeight);
return 0;
}
養成良好編碼習慣 (大括號風格、縮進、變量命名),利用編譯器警告 (`-Wall`)、靜態分析工具、單步調試,基礎錯誤迎刃而解。