1

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

8 回答 8

7

好吧,switchcases 与您给它的变量进行比较。因此,因为case 1它会尝试与您的陈述进行比较avg然后1它会继续您的陈述,当然,这永远不会适用。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;
于 2012-07-16T13:34:40.880 回答
7

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.

希望这会有所帮助。

于 2012-07-16T13:44:46.337 回答
3

该代码始终在您的默认语句中,因为您的 switch 语句将不会获得任何输入,以防万一。

于 2012-07-16T13:42:00.673 回答
2

您使用 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。

于 2012-07-16T13:34:21.400 回答
2

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

于 2012-07-17T14:04:59.663 回答
1

当你说

Switch(avg)

{
   case 1:
   break;
}

就像说一样if(avg == 1)。所以取下你的开关盒并像这样处理它

if ((avg >= 80) && (avg <= 100))
//do this
else if ((avg >= 70) && (avg <= 79))
//do that
于 2012-07-16T13:34:46.283 回答
0

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;
}

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

于 2012-07-16T13:44:26.290 回答
0

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

于 2012-07-16T14:08:00.430 回答