Switch case的行为与预期不符

时间:2014-07-09 00:34:28

标签: java android switch-statement

我所拥有的是一个用户将滑动的ImageView,并且在滑动时,一个名为plane的变量被更改,并且切换大小写用于我遇到问题的变量plane。这是我的代码:

(the below switch case is triggered on a swipe of the image view)


switch(plane){
            ...
            case 77:
                    Log.d("NICK","Case 77 Left Swipe");
                    plane = 78;
                    Picasso.with(context).load(R.drawable.plane77).into(image2);
                    plane_name.setText(plane_names[plane-1]);

                    break;
                case 78:
                    //********************************************************************
                    Log.d("NICK","Case 78 Left Swipe");
                    plane = 79;
                    Picasso.with(context).load(R.drawable.plane78).into(image2);
                    plane_name.setText(plane_names[plane-1]);

                    //*********************************************************************
                case 79:
                    Log.d("NICK","Case 79 Left Swipe");
                    plane = 80;
                    Picasso.with(context).load(R.drawable.plane79).into(image2);
                    plane_name.setText(plane_names[plane-1]);

                case 80:
                    Log.d("NICK","Case 80 Left Swipe ");
                    plane = 81;
                    Picasso.with(context).load(R.drawable.plane80).into(image2);
                    plane_name.setText(plane_names[plane-1]);

                case 81:
                    Log.d("NICK","Case 81 Left Swipe ");
                    plane = 82;
                    Picasso.with(context).load(R.drawable.plane81).into(image2);
                    plane_name.setText(plane_names[plane-1]);

                case 82:
                    Log.d("NICK","Case 82 Left Swipe ");
                    plane = 83;
                    Picasso.with(context).load(R.drawable.plane82).into(image2);
                    plane_name.setText(plane_names[plane-1]);

                case 83:
                    Log.d("NICK","Case 83 Left Swipe ");
                    plane = 84;
                    Picasso.with(context).load(R.drawable.plane83).into(image2);
                    plane_name.setText(plane_names[plane-1]);

                case 84:
                    Log.d("NICK","Case 84 Left Swipe ");
                    plane = 85;
                    Picasso.with(context).load(R.drawable.plane84).into(image2);
                    plane_name.setText(plane_names[plane-1]);

                ....
} 

问题是,无论出于何种原因,一旦达到案件78,案件78,79,80,81将在下面的印刷声明所示的同时触发。预期结果是一次触发1个案例,以便适当地改变plane的值。有些情况下最多77个相似,并且plane增加到始终为+1。这些先前的案例表现如预期。

运行时的Logcat输出:

07-08 20:21:24.010: DEBUG/NICK(9715): Case 72 Left Swipe
07-08 20:21:25.572: DEBUG/NICK(9715): Case 73 Left Swipe
07-08 20:21:27.183: DEBUG/NICK(9715): Case 74 Left Swipe
07-08 20:21:34.491: DEBUG/NICK(9715): Case 75 Left Swipe
07-08 20:21:36.944: DEBUG/NICK(9715): Case 76 Left Swipe
07-08 20:21:39.537: DEBUG/NICK(9715): Case 77 Left Swipe
//below cases 78,79,80,81 are triggered at the same time
07-08 20:21:42.600: DEBUG/NICK(9715): Case 78 Left Swipe
07-08 20:21:42.600: DEBUG/NICK(9715): Case 79 Left Swipe
07-08 20:21:42.600: DEBUG/NICK(9715): Case 80 Left Swipe
07-08 20:21:42.600: DEBUG/NICK(9715): Case 81 Left Swipe
07-08 20:21:42.610: DEBUG/NICK(9715): Case 82 Left Swipe
07-08 20:21:42.610: DEBUG/NICK(9715): Case 83 Left Swipe
07-08 20:21:42.610: DEBUG/NICK(9715): Case 84 Left Swipe

有什么可能导致此问题的建议?我怀疑这一定是一个逻辑错误,我忽略了但我无法发现任何错误。

5 个答案:

答案 0 :(得分:4)

正如其他答案所说,您错过了break;

但是,我高度建议重构此代码......

例如,对资源ID具有Map<Integer, Integer>映射平面编号。然后在启动时加载它:

Map<Integer, Integer> mImages = new HashMap<Integer, Integer>();
mImages.put(77, R.drawable.plane_77);
mImages.put(78, R.drawable.plane_78);
mImages.put(79, R.drawable.plane_79);
...

然后你可以用以下内容替换这个巨大的开关语句:

plane -= 1;
Picasso.with(context).load(mImageMap.get(plane)).into(image2);
plane_name.setText(plane_names[plane]);

答案 1 :(得分:3)

79,80等等你没有break;。所以解决这个问题:

case 79:
   Log.d("NICK","Case 79 Left Swipe");
   plane = 80;
   Picasso.with(context).load(R.drawable.plane79).into(image2);
   plane_name.setText(plane_names[plane-1]);
   break;

case 80:
   Log.d("NICK","Case 80 Left Swipe ");
   plane = 81;
   Picasso.with(context).load(R.drawable.plane80).into(image2);
   plane_name.setText(plane_names[plane-1]);
   break;

你看,break语句的作用是告诉它突破语句。如果没有break语句,则继续执行该行并执行80,然后是81,然后是82,依此类推等等。

答案 2 :(得分:1)

您需要在每个案例后添加break;

答案 3 :(得分:1)

这不是一种不寻常的行为。在案例77之后的代码块之后你忘了写中断。

答案 4 :(得分:0)

你有一个77的休息声明,但没有其他声明。这真的是你想要的吗?

C风格的开关/案例陈述对我来说似乎总是很愚蠢 - 99.9%的案件(双关语)你想要破解,为什么不把它作为默认值?

相关问题