我編寫對成績評級的代碼,利用條件運算符,可是執行代碼塊部分卻出現了問題,請指教!
編譯錯誤提示:缺少“;”(在“{”的前面)
代碼如下:
#include
#include
#include
using namespace std;
int main()
{
vector vec = { "E","D","C","B","A","A++" };
int grade;
string result;
cout << "請輸入你的成績:";
cin >> grade;
(grade < 0 || grade>100) ? {
cout << "該成績不合法" << endl;
return -1;
}
: (grade < 60)
result = vec[0];
:
{
result = vec[(grade - 50) / 10];
if ((grade % 10) <= 3 && (grade % 10)>0)
result += '-';
else if ((grade % 10) >= 7)
result += '+';
}
cout << result << endl;
return 0;
}
你這個問題好有意思,我改了一下你的代碼,大概弄出了這樣的代碼,不知道符不符合你的需求
#include <iostream>
#include <vector>
#include "string"
using namespace std;
int getResult(string &result,int grade) {
if ((grade % 10) <= 3 && (grade % 10)>0)
result += '-';
else if ((grade % 10) >= 7)
result += '+';
return 0;
}
int main()
{
string vec[] = { "E","D","C","B","A","A++" };
int grade;
string result;
cout << "請輸入你的成績:";
cin >> grade;
int b = (grade<0 || grade > 100)?(cout<<"該成績不合法"<<endl,-1):
(grade < 60?(result=vec[0],60)
:(result = vec[(grade - 50) / 10],getResult(result,grade),100));
if (b==-1) {
return -1;
}
cout << result << endl;
return 0;
}