【问题标题】:How to use IF in Switch statement如何在 Switch 语句中使用 IF
【发布时间】:2012-07-16 13:30:35
【问题描述】:

如何在 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");
}

【问题讨论】:

  • 我猜你误解了switch case的使用。卸下开关盒,您将获得所需的输出。
  • 感谢您的宝贵回复。我知道代码输入错误,但我想清除我只是 switch 语句。

标签: c++ switch-statement average


【解决方案1】:

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,我对我的解决方案感到非常困惑,因为我已经在 if & else if 中完成了此操作,但我想通过使用 switch 语句获得相同的输出。请让开关变得简单易行。问候
  • 是的,很明显。我感谢你的努力。请你告诉我如何接受分析器
  • @BinYousif 只需点击复选标记大纲即可;它会变绿
【解决方案2】:

好吧,switchcases 与您提供的变量进行比较。因此,对于case 1,它会尝试将avg1之后进行比较,它会继续使用你的if 语句,当然,这永远不会适用。

我不太确定你在那里尝试什么;从所有我可以看出你最好使用嵌套的if

if ((avg >= 80) && (avg <= 100))
{
  cout << " Your Average is: " << avg << endl;
  cout << "You Grade is A+ " << endl;
} else if ((avg >= 70) && (avg <= 79))
{
  cout << " Your Average is: " << avg << endl;
  cout << " Your grade is A " << endl;
} else if ((avg >= 60) && (avg <= 69))
{ 
  cout << " Your Average is: " << avg << endl;
  cout << " Your Grade is B+  " << endl;
} ...

但即使这样也可以更优雅地处理,这取决于您的技能。例如,您可以将平均值除以 10 并在其上使用 switch,利用整数除法截断的事实,将 5059 之间的所有内容折叠到同一个桶 5 中:

switch (avg / 10) {
  case 10:
  case 9:
  case 8:
    cout << " Your Average is: " << avg << endl;
    cout << "You Grade is A+ " << endl;
    break;
  case 7:
    cout << " Your Average is: " << avg << endl;
    cout << "You Grade is A " << endl;
  ...
}

您还会注意到您打印 »您的平均值是:...« 每次,因此您可以在条件之前移动该行:

cout << " Your Average is: " << avg << endl;
switch (avg / 10) {
  ...
}

最后,由于您有一个非常小的可能结果列表,您可以将它们收集在一个数组中并简单地引用适当的数组元素,甚至不需要if/else ifswitch

char[][] grades = {
  "F", "F", "F", "D", "C-", "C+", "B+", "A", "A+", "A+", "A+"
};
char[] grade;
if (avg > 100)
  grade = "F";
else
  grade = grades[max(0, avg / 10)];

cout << " Your average is " << avg << endl;
cout << " Your grade is " << grade << endl;

【讨论】:

  • 注意:所有代码都未经测试,我的 C/C++ 现在已经生锈了。但是您应该了解总体思路。欢迎指正:)
  • 亲爱的。感谢您的宝贵时间和宝贵的回复(完美回复)我解决了我的问题。
【解决方案3】:

代码总是在你的默认语句中,因为你的 switch 语句不会得到任何输入,以防万一。

【讨论】:

  • 感谢您的回复。你能更清楚地定义它吗?
【解决方案4】:

你使用 switch 有点错误,不是if 的问题。这个:

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

意思和

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

显然,条件永远不会满足。对于您的特定情况,您可以移除开关,只留下ifs。

【讨论】:

  • 兄弟,我现在明白了,我删除了这个案例6的条件:(avg >= 30) && (avg
【解决方案5】:

删除Switch- case,你会得到你想要的输出,因为在这种情况下它总是默认的。

【讨论】:

    【解决方案6】:

    当你说

    Switch(avg)
    
    {
       case 1:
       break;
    }
    

    if(avg == 1) 相同。所以移除你的开关盒并像这样处理它

    if ((avg >= 80) && (avg <= 100))
    //do this
    else if ((avg >= 70) && (avg <= 79))
    //do that
    

    【讨论】:

    • 如何在 switch 中做到这一点。我是否必须创建所有案例。例如从 1. 到 100..
    【解决方案7】:

    switch 语句将 1、2、3 等 case 情况识别为 avg 的值。

    您只能使用带有特定值的 switch() 控件,而不是间隔。在这种情况下,我建议您使用 else if{} 结构。

    或者您可以在多种情况下实现 1 条语句(但不可读)。

    switch(avg){
       case 80: case 81: case 82:   //do this until 100
    
          cout << " Your Average is: " << avg << endl;
          cout << "You Grade is A+ " << endl;
          break;
    }
    

    而且您不需要为每种情况使用范围(大括号)。

    【讨论】:

    • 兄弟让我试试这个。希望对我有帮助
    【解决方案8】:

    最好也可能是最简单的做法是去掉 switch 语句并使用“if”和“elseif”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多