C++判断成绩等级

2022/1/28 20:10:14

本文主要是介绍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++判断成绩等级的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程