1. 给出一个百分制成绩,要求输出成绩等级A、B、C、D和E。90分以上为A,89~80为B,79~70为C,69~60为D

要详细的过程可以用if语句吗?还是非常感谢!
2024-11-17 16:28:13
推荐回答(1个)
回答1:

#include
using namespace std;

void main(){
cout<<"请输入成绩";
int score;
cin>>score;
while(score<0||score>100){
cout<<"您输入的成绩不符合要求,请重新输入";
cin>>score;
}
switch(score/10){
case 10:
case 9:
cout<<"A";
break;
case 8:
cout<<"B";
break;
case 7:
cout<<"C";
break;
case 6:
cout<<"D";
break;
default:
cout<<"E";
}
system("pause");
}

这种情况是标准的使用switch的情况,用if也行,更简单:
#include
using namespace std;

void main(){
cout<<"请输入成绩";
int score;
cin>>score;
while(score<0||score>100){
cout<<"您输入的成绩不符合要求,请重新输入";
cin>>score;
}
if(score>=90){
cout<<"A";
}else{
if(score>=80){
cout<<"B";
}else{
if(score>=70){
cout<<"C";
}else{
if(score>=60){
cout<<"D";
}else{
cout<<"E";
}
}

}
}
system("pause");
}