用else if
#include <iostream>
using namespace std;
int main() {
double weight;
double height;
cin >> weight;
cin >> height;
double BMI = weight / (height*height);
if(BMI<18.5){
cout << "偏瘦";
}
else if(BMI>=18.5&&BMI<20.9){
cout<<"苗条";
}
else if(BMI>=20.9&&BMI<24.9){
cout<<"适中";
}
else{
cout<<"偏胖";
}
return 0;
}
用三目运算符
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
double weight;
double height;
cin >> weight;
cin >> height;
double BMI = weight / (height*height);
string state = (BMI>24.9) ? "偏胖":
(BMI>=20.9)?"适中":
(BMI>=18.5)?"苗条":
"偏瘦";
cout << state;
return 0;
}