#include
using namespace std;
int main()
{
int i;
int a[4] = { 0 };
for(i=0;i<4;i++)
cin >> a[i];
if (a[i] >= 90 && a[i] <= 100)
cout <<a[i]<< "优"<<endl ;
else if (a[i] >= 80 && a[i] < 90)
cout << a[i]<<"良"<<endl ;
else if (a[i] >= 60 && a[i] < 80)
cout << a[i]<<"中" <<endl;
else
cout << a[i]<<"差"<<endl ;
return 0;
}
你用for循环输入了4个数,可后面的if...else并没有用循环来处理,自然只能处理一个数的结果。而且循环结束后,i值为4,a[i]是越界访问啊。
#include <iostream>
using namespace std;
int main()
{
int i;
int a[4] = { 0 };
for(i=0;i<4;i++)
cin >> a[i];
for(i=0;i<4;i++)
{
if (a[i] >= 90 && a[i] <= 100)
cout <<a[i]<< "优"<<endl ;
else if (a[i] >= 80 && a[i] < 90)
cout << a[i]<<"良"<<endl ;
else if (a[i] >= 60 && a[i] < 80)
cout << a[i]<<"中" <<endl;
else
cout << a[i]<<"差"<<endl ;
}
return 0;
}
或者:
#include <iostream>
using namespace std;
int main()
{
int i;
int a[4] = { 0 };
for(i=0;i<4;i++)
{
cin >> a[i];
if (a[i] >= 90 && a[i] <= 100)
cout <<a[i]<< "优"<<endl ;
else if (a[i] >= 80 && a[i] < 90)
cout << a[i]<<"良"<<endl ;
else if (a[i] >= 60 && a[i] < 80)
cout << a[i]<<"中" <<endl;
else
cout << a[i]<<"差"<<endl ;
}
return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!