【发布时间】:2016-01-14 14:09:36
【问题描述】:
我正在尝试将其转换为 switch 语句
if (codeSection == 281)
cout << "bigamy";
else if (codeSection == 321 || codeSection == 322)
cout << "selling illegal lottery tickets";
else if (codeSection == 383)
cout << "selling rancid butter";
else if (codeSection == 598)
cout << "wounding a bird in a public cemetery";
else
cout << "some other crime";
// Actual switch statement
switch (codeSection)
{
case '281':
cout << "bigamy" << endl;
break;
case '321':
case '322':
cout << "selling illegal lottery tickets" << endl;
break;
case '383':
cout << "selling rancid butter" << endl;
break;
case '598':
cout << "wounding a bird in a public cemetery";
break;
default:
cout << "some other crime"<< endl;
}
编译器说 switch 语句多字符常量,并给我一个黄色警告,但仍然编译。我的问题是这种情况应该只是 char 形式吗?像案例'2'
【问题讨论】:
-
您可能应该知道编译器 警告 是您应该考虑的事情,但不一定会破坏任何东西。事实上,许多警告都可以关闭(并非必须如此)。另一方面,错误会阻止您的代码正确编译或运行。
标签: c++ switch-statement