這是我工作的任務:只返回第一條if語句? (C++)
寫,提示用戶輸入贏得了一個療程的點,並確定等級的程序。該程序應顯示等級和等級的消息字符串。使用'if else'語句來確定打印消息字符串的等級和switch語句。
我編寫了代碼,它執行時沒有任何調試錯誤。問題是,無論我輸入什麼作爲我的PointsEarned
,我只得到F的輸出!例如,如果我輸入「90」:
Try Harder Next Time.
GRADE: F
什麼問題?我是否把我的switch語句放在錯誤的地方?代碼如下:
#include <iostream>
using namespace std;
int main()
{
int PointsEarned;
char Grade;
cout << "Please input the points earned in the course: ";
cin >> PointsEarned;
if (0 <= PointsEarned < 60) {
Grade = 'F';
}
else if (60 <= PointsEarned < 70) {
Grade = 'D';
}
else if (70 <= PointsEarned < 80) {
Grade = 'C';
}
else if (80 <= PointsEarned < 90) {
Grade = 'B';
}
else if (90 <= PointsEarned) {
Grade = 'A';
}
else{
cout << "That is not a valid input.";
}
switch (Grade)
{
case 'F':
case 'D':
cout << "Try Harder Next Time." << endl;
break;
case 'C':
cout << "Good." << endl;
break;
case 'B':
cout << "Very good." << endl;
break;
case 'A':
cout << "Excellent." << endl;
break;
default:
cout << "Please choose a valid input to receive your grade." << endl;
}
cout << "GRADE: " << Grade << endl;
}
I''m敢肯定你需要的東西就像'if(0 <= PointsEarned && 60> PointsEarned)'一樣。 –
另請參閱http://stackoverflow.com/questions/8889522/is-4-y-1-a-valid-statement-in-c-how-do-you-evaluate-it-if-so – iksemyonov