【发布时间】:2020-11-08 15:19:10
【问题描述】:
所以这是我试图开始工作的代码,但它在到达 B 时显示“重复大小写值” 我是大学一年级的学生,所以我可能使用了错误的格式,或者我可能忽略了一些我似乎真的没有弄清楚问题的地方,所以我向你们寻求帮助
char dep;
int exp;
cout<<"please enter your department, A, B OR C: ";
cin>>dep;
cout<<"please enter your years of experience ";
cin>>exp;
switch(dep)
{
case 'A' || 'a' :{
switch (exp) {
case 5:
cout<<"you will recieve a 5% raise and 2.5% extra due to your experience";
break;
defualt : cout<<"you get 5% raise";
break;
}
}
break;
case 'B' || 'b' :{
switch (exp) {
case 5:
cout<<"you will recieve a 2% raise and 2.5% extra due to your experience";
break;
defualt : cout<<"you get 2% raise";
break;
}
}
break;
【问题讨论】:
-
case 'A' || 'a'是错误的。使用 2 个 case 代替 case 'A`: case 'a': -
case 'A' || 'a'- 不是语言的工作方式。你需要两个案例。一个可能是另一个。例如case 'A'; case 'a':正如所写的那样,您对true有两个测试。仅供参考,嵌套与此无关。 -
'A' || 'a'和'B' || 'b'都为真(非零值被隐式转换为真),所以你得到两个case true:,编译器输出错误。 -
对于这么简单的事情,我宁愿推荐
dep = std::tolower(dep); if (dep == 'a') { /* Code for A */ } else if (dep == 'b') { /* Code for B */ } else { /* Neither */ } -
@Someprogrammerdude 他们当然应该写一个。
标签: c++ switch-statement valueerror