gpt4 book ai didi

c++ - 如何在 Switch 语句中使用 IF

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:05:06 27 4
gpt4 key购买 nike

如何在 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 语句用于根据特定值执行一段代码。从某种意义上说,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.

希望对您有所帮助。

关于c++ - 如何在 Switch 语句中使用 IF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11505438/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com