源代码
#include<iostream>//程序中有输入/输出就要有iostream头文件
using namespace std;
int main()
{
int n;
cout << "Enter the score:";
cin >> n;
if (n >= 60)
if (n >= 90)
cout << "The degree is A" << endl;
else if(n>=80)
cout << "The degree is B" << endl;
else
cout << "The degree is C" << endl;
else
cout << "The degree is D" << endl;
return 0;
}
具体分析
本次代码主要使用的是if-else语句。
// if (n >= 60)
{ }
else
cout << "The degree is D" << endl;
这个是大判断,如果输入的成绩大于60,则进行大括号内的内容,否则,执行else语句:输出The degree is D。
// if (n >= 90)
cout << "The degree is A" << endl;
else if(n>=80)
cout << "The degree is B" << endl;
else
cout << "The degree is C" << endl;
这部分判断的前提条件是n>60,依顺序依次执行,满足条件则输出。
运行结果
输入55,因为55<60,所以执行
else
cout << “The degree is D” << endl;部分的代码,输出结果为The degree is D。
输入65,因为60<65<80,所以执行
else
cout << “The degree is C” << endl; 部分的代码,输出结果为The degree is C。
输入85,因为60<80<85<90,所以执行
else if(n>=80)
cout << “The degree is B” << endl;部分的代码,输出结果为The degree is B。
输入95,因为60<<90<95,所以执行
if (n >= 90)
cout << “The degree is A” << endl;部分的代码,输出结果为The degree is A。