如何在Switch语句中应用if条件,我想计算Average:但是我已经尽力解决问题,但仍然没有得到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");
}
答案 0 :(得分:7)
好吧,switch
将case
s与你给它的变量进行比较。因此,对于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
,利用整数除法截断的事实,将50
和59
之间的所有内容折叠到同一个存储区{{1} }:
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
或{{1 }}:
else if
答案 1 :(得分: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之间,假设平均值介于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.
希望这有用。
答案 2 :(得分:3)
代码始终在您的默认语句中,因为您的switch语句将不会获得任何输入,如大小写所示。
答案 3 :(得分:2)
您使用的开关有点不对,这不是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;
}
}
显然,这种情况永远不会得到满足。对于您的特定情况,您只需删除开关,只留下if
s。
答案 4 :(得分:2)
删除Switch- case
,您将获得所需的输出,因为在这种情况下,它始终是默认输出。
答案 5 :(得分:1)
当你说
时Switch(avg)
{
case 1:
break;
}
与if(avg == 1)
相同。所以移除你的开关盒并像这样处理它
if ((avg >= 80) && (avg <= 100))
//do this
else if ((avg >= 70) && (avg <= 79))
//do that
答案 6 :(得分:0)
通过switch语句将1,2,3等案例情况识别为avg的值。
您只能将switch()控件用于某些值,而不是间隔。在这种情况下,我建议你使用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;
}
你不需要为每个案例使用范围(大括号)。
答案 7 :(得分:0)
最好也可能最容易做的就是摆脱switch语句并使用“if”和“elseif”。