如何應用if語句中的條件,我想計算Average:但是我試過最好解決問題但仍然沒有從Switch語句輸出。 我剛使用C++,如何在Switch語句中使用IF
#include <iostream>
using namespace std;
int main()
{
//declaration of variables.
int sub1,sub2,sub3, sub4,sub5,total,avg;
//accepting marks from user in each subject
cout << " Enter Programming in C++ Marks: " << endl;
cin >> sub1;
cout << " Enter Software Engineering Marks : " << endl;
cin >> sub2;
cout << " Enter Personal Communication Marks : " << endl;
cin >> sub3;
cout << " Enter Database Application Marks: " << endl;
cin >> sub4;
cout << " Enter Computer Concept Marks: " << endl;
cin >> sub5;
//calculatin sum of marks obtained in each subject
total = (sub1 + sub2 + sub3 + sub4 + sub5);
//calculating the average marks
avg = total/5;
// starting of if condition for finding out grades of total subjects.
switch (avg){
case 1:{
if ((avg >= 80) && (avg <= 100))
{
cout << " Your Average is: " << avg << endl;
cout << "You Grade is A+ " << endl;
}
break;
}
case 2:{
if ((avg >= 70) && (avg <= 79))
{
cout << " Your Average is: " << avg << endl;
cout << " Your grade is A " << endl;
}
break;
}
case 3:{
if ((avg >= 60) && (avg <= 69))
{
cout << " Your Average is: " << avg << endl;
cout << " Your Grade is B+ " << endl;
}
break;
}
case 4:{
if ((avg >= 50) && (avg <= 59))
{
cout << " Your Average is: " << avg << endl;
cout << " Your Grade is C+ " << endl;
}
break;
}
case 5: {
if ((avg >= 40) && (avg <= 49))
{
cout << " Your Average is: " << avg << endl;
cout << " Your Grade is C- ! " << endl;
}
break;
}
case 6: {
if ((avg >= 30) && (avg <= 39))
{
cout << " Your Average is: " << avg << endl;
cout << " Your Grade is D ! " << endl;
}
break;
}
default:{
if ((avg >= 100) && (avg <= 29))
{
cout << " Your Average is: " << avg << endl;
cout << " Your Grade is F, So you are Fail in the class ! " << endl;
break;
}
}
}
system("pause");
}
我想你錯過了開關盒的使用。取下開關盒,你會得到所需的輸出。 – brano 2012-07-16 13:34:19
感謝您的寶貴回覆。我知道代碼是錯誤的鍵入,但我想清除我只是切換語句而已。 – 2012-07-17 17:11:12