解题思路:
用switch()语句,正好可以用分数/10的整数值作为case。
注意事项:
需要做异常处理,保证分数在0-100以内。
参考代码:
#include <iostream> using namespace std; int main() { int score = 0; int rank = 0; cin >> score; if(score < 0 || score > 100) { return -1; } switch(score / 10) { case 10: cout << "A" << endl; break; case 9: cout << "A" << endl; break; case 8: cout << "B" << endl; break; case 7: cout << "C" << endl; break; case 6: cout << "D" << endl; break; case 5: cout << "E" << endl; break; case 4: cout << "E" << endl; break; case 3: cout << "E" << endl; break; case 2: cout << "E" << endl; break; case 1: cout << "E" << endl; break; case 0: cout << "E" << endl; break; default: return -1; } return 0; }
0.0分
2 人评分
用if else不好吗qwq #include <iostream> using namespace std; int main(){ int grade; cin>>grade; if (grade<=100 && grand>=90) cout<<"A"<<endl; else if (grade<90 && grade>=80) cout<<"B"<<endl; else if (grade<80 && grade>=70) cout<<"C"<<endl; else if (grade<70 && grade>=60) cout<<"D"<<endl; else if (grade<60 && grade>=0) cout<<"E"<<endl; return 0; }