2014-02-25 130 views
3

我一直在一臺交換機上的最後一個半小時的整數現在,我知道如何與焦炭做交換機,但這看起來十分辛苦me.any建議將appreciated.the問題我有我不能接受超過100這個開關目前的分數C++ INT switch語句

int testScore;      
    cout <<"Enter your test score and I will tell you \n"; 
    cout <<"the letter grade you earned "; 
    cin >> testScore; 

    switch(testScore/10) 
{ 
    case 10: 
    case 9: 
     cout <<"Your grade is A.\n"; 
    break; 
    case 8: 
     cout <<"Your grade is B.\n"; 
    break; 
    case 7: 
     cout <<"Your grade is C.\n"; 
     break; 
    case 6: 
      cout << "Your grade is D.\n"; 
     break; 
    case 5: 
      cout << "Your grade is F.\n"; 
     break; 

    default: 
     cout << "That score isn’t valid\n"; 

    } 
+1

在浮法的開關[甚至不應該編譯](http://coliru.stacked-crooked.com/a/a78e84e21d532797),所以我不確定它如何能夠接受100以上的分數。 – Borgleader

+3

如果您需要檢查值範圍*,交換機通常是該作業的錯誤工具。輸入「90」後你會發生什麼? –

+0

得分爲110會導致「案例」值爲11,除了「默認」情況外,您沒有考慮過這種情況。 –

回答

3

你除以10.0,這是一個雙重,這將不會編譯。這必須更改爲10. 此外,您應該在switch語句之前加上if語句來檢查它是否在有效範圍內。

#include<iostream> 
#include<iomanip> 
using namespace std; 
int main() 
{ 

int testScore; 
cout <<"Enter your test score and I will tell you \n"; 
cout <<"the letter grade you earned \n"; 
cin >> testScore; 

if (testScore<=100 && testScore>=0) 
    switch(testScore/10) 
    { 
     case 10: 
     case 9: 
      cout <<"Your grade is A.\n"; 
      break; 
     case 8: 
      cout <<"Your grade is B.\n"; 
      break; 
     case 7: 
      cout <<"Your grade is C.\n"; 
      break; 
     case 6: 
      cout << "Your grade is D.\n"; 
      break; 
     case 5: 
      cout << "Your grade is F.\n"; 
      break; 

     default: 
      cout << "That score isn’t valid\n"; 
    } 
else 
    cout <<"That score isn't valid\n"; 

return 0; 
} 
+0

我想這和它仍然顯示我的一對數字100-109 –

+3

這應該是一個評論,這並不回答這個問題一個檔次。 – jliv902

+2

在C++(以及許多其他語言)中,將整數「109」除以整數「10」將返回「10」,這將觸發第一個「case」。如果你不想讓人們輸入大於'100'的數字,你需要在某個地方強制執行。例如,在你使用'cin'和'switch'語句的行之間。 –

0

我決定做這種方式,感謝所有的答案

int testScore;      
    cout <<"Enter your test score and I will tell you \n"; 
    cout <<"the letter grade you earned "; 
    cin >> testScore; 

if (testScore >= 0 && testScore <=100) 
{ 

     switch(testScore/10) 
    { 
     case 10: 
     case 9: 
      cout <<"Your grade is A.\n"; 
     break; 
     case 8: 
      cout <<"Your grade is B.\n"; 
     break; 
     case 7: 
      cout <<"Your grade is C.\n"; 
      break; 
     case 6: 
       cout << "Your grade is D.\n"; 
      break; 
     default: 
       cout << "Your grade is F.\n"; 

     } 

} 

else 
    cout <<"That score isn't valid" << endl; 
1

你真的應該用「如果」不是「開關」做到這一點。類似下面的代碼(未測試):

if (testScore >=0 && testScore <= 100) 
{ 
    char grade; 

    if (testScore >= 90) 
     grade = 'A'; 
    else if (testScore >= 80) 
     grade = 'B'; 
    else if (testScore >= 70) 
     grade = 'C'; 
    else if (testScore >= 60) 
     grade = 'D'; 
    else 
     grade = 'F'; 

    cout << "Your grade is " << grade << endl; 
} 
else 
{ 
    cout << "Score of " << testScore << " is not valid" << endl; 
}