我在调试的文件中发现了一个令人讨厌的复制粘贴错误.它是switch声明中的重复案例标签.
switch (value) {
case 1:
case 2:
doSomething();
break;
case 2:
doSomethingElse();
break;
default:
break;
}
Run Code Online (Sandbox Code Playgroud)
事实证明,这是完全合法的JavaScript.甚至标准的代码质量工具也没有在这里抱怨:JSHint和JSCS对于重复标签,缺少的break语句和第二种情况2之后的死代码都没问题.
据我所知,重复的案例值在C++和Java中是非法的.
JavaScript中的交换机块中是否有多个相同的案例标签?为什么允许这样做?
我switch在我的C++代码中有一个语句,并且想在该语句中声明并使用一个变量case.该变量仅在该特定范围内使用case.
switch(mode)
{
case abc:
...
struct commonData;
commonData = manager->getDataByIndex(this->Data.particularData);
int someInt = 1;
...
break;
case xyz:
...
commonData = Manager->getDataByIndex(this->Data.particularData);
break;
default:
...
break;
}
Run Code Online (Sandbox Code Playgroud)
我试过简单地声明,初始化和使用变量(int someInt)就在里面case,但这给了我一些编译错误......在SO上遇到这个问题:为什么变量不能在switch语句中声明?,我尝试做了答案的建议,并添加{}到case问题中,所以我switch现在看起来像这样:
switch(mode)
{
case abc:
{
...
struct commonData;
commonData = manager->getDataByIndex(this->Data.particularData);
int someInt = 1;
...
break;
}
case xyz:
...
commonData = manager->getDataByIndex(this->Data.particularData);
break;
default:
...
break;
}
Run Code Online (Sandbox Code Playgroud)
但我现在收到编译错误,说明:在一个变量(commonData)的"未声明的标识符",在该 …
我想在C++的switch/case语句中使用const int数组.可能吗?到目前为止,我尝试过类似的东西:
int main()
{
int const tab[3] = {1,2,3};
int value(2);
switch(value)
{
case tab[1]:
cout << "result is: " << tab[0]<< endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
然而,编译器一直告诉我:
.../main.cpp|11|error: the value of ‘tab’ is not usable in a constant expression
Run Code Online (Sandbox Code Playgroud)
我把我的数组声明为"int const",这还不够吗?
我正在尝试根据内容交换某些特定div的背景颜色.
当他们的输入类似于"生活方式"或"政治"或"经济"或"本地"或"体育"或"新闻"时,他们的背景颜色应该互换.
var colorInput = document.getElementById('views-field-field-section').textContent;
switch (colorInput) {
case 'Lifestyle':
document.getElementById('views-field-field-section').style.backgroundColor = '#9518b8';
break;
case 'Local':
document.getElementById('views-field-field-section').style.backgroundColor = '#009fe3';
break;
case 'Sports':
document.getElementById('views-field-field-section').style.backgroundColor = '#95c11f';
break;
case 'Economy':
document.getElementById('views-field-field-section').style.backgroundColor = '#d40d10';
break;
case: 'Politics':
document.getElementById('views-field-field-section').style.backgroundColor = '#ffcc00';
break;
default:
break;
}
Run Code Online (Sandbox Code Playgroud)
byte a = 125;
final byte b = 2;
final Byte c = 3;
switch (a) {
case b: // works fine
break;
case c: // Constant Expression required
break;
}
Run Code Online (Sandbox Code Playgroud)
既然c是final变量,它不是编译时常量,因此是有效的案例标签吗?
我是golang的新手,我正在尝试创建一个函数,该函数基于使用的结构,将使用Sprintf返回格式化的字符串
type Name struct {
Title string
First string
Last string
}
type Location struct {
Street string
City string
State string
Zip string
}
func Merge(m interface{}) string {
switch m.(type) {
case *Location:
return fmt.Sprintf("%s \n %s, %s %s", m.(*Location).Street, m.(*Location).City, m.(*Location).State, m.(*Location).Zip)
case *Name:
return fmt.Sprintf("%s. %s %s", m.(*Name).Title, m.(*Name).First, m.(*Name).Last)
}
return "Not Applicable"
}
fmt.Println(Merge(Location))
Run Code Online (Sandbox Code Playgroud)
我正在Not Applicable从我的邮件中得到“ ”信息PrintLn。在该代码的一个版本中,我认为消息是“ out of index”。
我在Swift中有一个switch语句,如下所示:
switch tuple {
case (let someObject, let current, nil):
return true
// Other cases...
}
Run Code Online (Sandbox Code Playgroud)
元组的类型的(SomeObject?, SomeObject, SomeObject?),和我说的英语是:匹配其中前两个元素的情况下不为零,而第三个(可选的)是零.
Xcode的7告诉我,因为我没有使用绑定someObject和current,我应该用下划线代替它.但是如果我用下划线替换元组中的第一个元素,它是否也会匹配第一个元素为nil的情况,因为这_意味着编译器会忽略该值?对于第一个元素为零的情况,我有一个单独的案例.
为了记录,看起来我的代码仍然按照我的预期工作,但我想确定,我无法在任何地方找到任何文档.
我目前正在使用Ruby为银行管理系统编写程序.该系统的一个功能是它可以创建一个新帐户,帐户可以是六种类型之一.
我的控制器中有以下方法来满足此功能:
def create_account(type, holder)
case type
when :current then CurrentAccount.new(holder, @account_number)
when :savings then SavingsAccount.new(holder, @account_number)
when :business then BusinessAccount.new(holder, @account_number)
when :ir then IRAccount.new(holder, @account_number)
when :smb then SMBAccount.new(holder, @account_number)
when :student then StudentAccount.new(holder, @account_number)
end
end
Run Code Online (Sandbox Code Playgroud)
这些账户中的每一个都从基础账户继承,最终将包含个人属性,例如利率,透支等.
虽然这是功能性的,并提供了所需的结果,但感觉有点冗长.但是我想不出任何明显的重构方法.
欢迎任何建议......
是否有类似case语句的数组,并将其作为单个case语句放入switch-假设
String[] statements={"height","HEIGHT"};
Run Code Online (Sandbox Code Playgroud)
然后
switch(code){
case statements:
//code here
break;
case something_else:
break;
}
Run Code Online (Sandbox Code Playgroud)
所以如果我们在String数组中添加值,那么它会自动匹配switch中的那个数组吗?喜欢
var1||var2||whatever //accessed from array or anything else for matching
Run Code Online (Sandbox Code Playgroud)
是否有类似这样的实现?
以下代码是有效的matlab sytax,用于检查是否b匹配中的任何元素a.但是,如果代码用于代码生成(即simulink),那么我得到错误:
'code generation only supports cell operations for varargin and varargout
Run Code Online (Sandbox Code Playgroud)
您可以通过%#codegen在脚本顶部添加来检查这一点.)
a={2 3};
b=3;
switch b
case a
disp yay
otherwise
disp boo
end
Run Code Online (Sandbox Code Playgroud)
case在代码生成兼容代码中,我应该如何匹配语句中的多个模式?
以下内容对我不起作用:
case a(1) || a(2) %//with a=[2, 3] above, since cells not allowed
case a(:)
Run Code Online (Sandbox Code Playgroud) matlab code-generation simulink switch-statement matlab-coder