首页 > 代码库 > 3.百分制成绩转换为5个等级输出

3.百分制成绩转换为5个等级输出

(1)采用IF语句嵌套:
#include <iostream>
using namespace std;

int main()
{
    int a;
    cout<<"please input your score: "<<endl;
    cin>>a;
    if(a>100||a<0)
        cout<<"wrong input! please try again!"<<endl;
    else if(a>=90&&a<=100)
        cout<<"you get an A! you are doing great job!"<<endl;
    else if(a>=80&&a<90)
        cout<<"you get a B! keep going!"<<endl;
    else if(a>=70&&a<80)
        cout<<"you get a C! It‘s OK,you can do better!"<<endl;
    else if(a>=60&&a<70)
        cout<<"you get an E! you pass the exam."<<endl;
    else if(a<60)
        cout<<"you did‘not pass the exam,but don‘t lose heart."<<endl;

    return 0;

}
 
(2)采用switch语句:
#include <iostream>
using namespace std;

int main()
{
    int score,temp;//先定义,后使用
    char grade;//注意定义等级时须定义为字符型
    cout<<"please input your score: "<<endl;
    cin>>score;

    temp=(int)score/10;//强制类型转换

    switch(temp)//输入之前的数据
    {
        case 10:grade=‘A+‘;break;
        case 9:grade=‘A‘;break;
        case 8:grade=‘B‘;break;
        case 7:grade=‘C‘;break;
        case 6:grade=‘D‘;break;
        default :grade=‘E‘;break;//其他默认结果输出,冒号不能忘
    }
    cout<<"you grade is: "<<grade<<endl;

    return 0;
}