法一:
#include<iostream>
using namespace std;
int main()
{
int score;
cout << "请输入学生分数:" << endl;
cin >> score;
if (score >= 90 && score <= 100)
{
cout << "优秀" << endl;
}
else if (score >= 80 && score <= 89)
{
cout << "良好" << endl;
}
else if (score >= 80 && score <= 89)
{
cout << "良好" << endl;
}
else if (score >= 70 && score <= 79)
{
cout << "中等" << endl;
}
else if (score >= 60 && score <= 69)
{
cout << "及格" << endl;
}
else
{
cout << "不及格" << endl;
}
return 0;
}
法2:
#include<iostream>
using namespace std;
int main()
{
int score;
cout << "请输入学生分数:" << endl;
cin >> score;
switch (score / 10)
{
case 10:
case 9:
cout << "优秀" << endl;
break;
case 8:
cout << "良好" << endl;
break;
case 7:
cout << "中等" << endl;
break;
case 6:
cout << "及格" << endl;
break;
default:
cout << "不及格" << endl;
break;
}
return 0;
}
该博客展示了两种不同的C++编程方法,用于根据输入的学生分数判断其等级:优秀、良好、中等、及格或不及格。第一种方法使用if...else语句,第二种方法利用switch...case结构。这两种实现都是对学生分数进行区间判断的经典示例。
2847

被折叠的 条评论
为什么被折叠?



