如何在 Switch 语句中使用 IF

新手上路,请多包涵

如何在 Switch 语句中应用 if 条件,我想计算平均值:但是我已经尽力解决这个问题,但仍然没有从 Switch 语句中得到输出。我是 C++ 的初学者,

 #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");
}

原文由 Mustafa Muhammad Yousif 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 590
2 个回答

switch 语句用于根据特定值执行一段代码。从某种意义上说,switch 语句可以被认为是 if 语句的一种形式:代码

switch (avg) {
   case 1 : { /* code block 1 */ } break;
   case 2 : { /* code block 2 */ } break;
   default : { /* code block default */ } break;
}

可以读作

if (1 == avg) { /* code block 1 */ }
else if (2 == avg) { /* code block 2 */ }
else { /*code block default */ }

您的 switch 语句可以读作

if (1 == avg)
{
   if ((avg >= 80) && (avg <= 100))
   {
      cout << " Your Average is: " << avg << endl;
      cout <<  "You Grade is A+ " << endl;
   }
} else if...

并且 avg 不可能 == 1 并且大于 80 且小于 100,这就是为什么你没有得到任何输出的原因。

在 C++ 中,switch 语句不适合测试范围。我只会使用 if 语句:

 if ( (avg<=100) && (avg >=80))
{
   // you get an A
} else if ...

但是,如果您真的需要使用开关,有几种方法可以解决:

 switch (avg) {
   case 100:
   case 99:
   case 98:
   ...
   case 80 : { /* you get an A+ */ break; }
   case 79 :
   case 78 :
   ...
   case 70 : { /* you get a A */ break: }
etc.

这很难看,但它 一个 switch 语句。使用 switch 语句的更简洁的方法可能是将 avg “强制”为离散值,而不是范围。

 int percentile = avg / 10;  // integer division.

现在范围将在 0 到 10 之间,假设 avg 在 0 到 100 之间。这将使开关变得更清晰:

 switch (percentile) {
   case 10 :
   case 9:
   case 8: /* You get an A+ */ break;
   case 7: /* You get an A */ break;
   case 6: /* You get a B+ */ break;

etc.

希望这会有所帮助。

原文由 brismith 发布,翻译遵循 CC BY-SA 3.0 许可协议

开关(平均){

         case 80 ... 100:

        cout << " Your Average is: " << avg << endl;
        cout << "You Grade is A+ " << endl;

    break;
  }

原文由 Rubenson Eduria 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题