C++判断成绩等级

else if

#include <iostream>
using namespace std;

int main() {
    
    int score;
    cin >> score;
    
    if (score>=90 && score<=100)
        cout << "优秀";
    else if (score>=80 && score<=89)
        cout << "良";
    else if (score>=70 && score<=89)
        cout << "中";
    else if (score>=60 && score<=69)
        cout << "及格";
    else if (score>=0 && score<=59)
        cout << "差";
    else
        cout << "成绩不合法";
       

    return 0;
}

三目运算符

#include <iostream>
using namespace std;

int main() {
    
    int score;
    cin >> score;
    string level =  (score<0 || score>100) ? "成绩不合法":
                        (score>=90) ? "优秀":
                            (score>=80) ? "良":
                                (score>=70) ? "中":
                                    (score>=60) ? "及格":
                                        "差";
    cout << level << endl;
      
    return 0;
}
上一篇:C# dbml 反转为表结构


下一篇:Note -「模板」矩阵 - 行列式