运行此代码
var myValue1:int = 2;
switch (myValue1)
{
case -3: trace(myValue1 + " == -3"); break;
case -2: trace(myValue1 + " == -2"); break;
case -1: trace(myValue1 + " == -1"); break;
case 0: trace(myValue1 + " == 0"); break;
case 1: trace(myValue1 + " == 1"); break;
case 2: trace(myValue1 + " == 2"); break;
case 3: trace(myValue1 + " == 3"); break;
default: trace(myValue1 + " is unknown"); break;
}
var myValue2:int = -2;
switch (myValue2)
{
case -3: trace(myValue2 + " == -3"); break;
case -2: trace(myValue2 + " == -2"); break;
case -1: trace(myValue2 + " == -1"); break;
case 0: trace(myValue2 + " == 0"); break;
case 1: trace(myValue2 + " == 1"); break;
case 2: trace(myValue2 + " == 2"); break;
case 3: trace(myValue2 + " == 3"); break;
default: trace(myValue2 + " is unknown"); break;
}
给出这个输出:
2 == 0 -2 未知
(在 Flash Builder 4.7.0.349722 中编译,在 Flash 11.5.502.149 上运行。在 Windows 7 SP1、Firefox 18.0.2 中运行)
以下更改都解决了上述问题,并提供了正确的输出:
- 将值类型更改为
Number
. - 删除负数
case
语句。 - 将语句更改
case
为使用int
-变量而不是文字......除非这些变量也是const
,在这种情况下它会被破坏!
改变myValue2 = -1
给出了输出-1 == -3
,这同样是 wtf-ish。
显然这是一个错误,但是......是什么原因造成的?int
在我不理解的案例陈述中使用或负数是否有一些微妙的细微差别?我的代码有什么错误吗?或者这仅仅是 Flash Builder 中字节码编译器的问题?