Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 开关外壳的行为不符合预期_Java_Android_Switch Statement - Fatal编程技术网

Java 开关外壳的行为不符合预期

Java 开关外壳的行为不符合预期,java,android,switch-statement,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")

我所拥有的是一个用户将滑动的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个案例,以便适当更改平面的值。有多达77种情况类似,它们将平面增量始终为+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

对于导致此问题的原因有何建议?我怀疑这一定是我忽略了的一个逻辑错误,但我在我所做的事情中找不到任何错误。

你有77的中断声明,但其他的没有。这真的是你想要的吗

C风格的开关/案例陈述在我看来总是愚蠢的——99.9%的案例都是双关语,你想要突破,为什么不把它设为默认值?

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

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,依此类推。

您需要添加一个break;在每一种情况下

这并不是一种不寻常的行为。您忘记在案例77后的代码块后面写break。

正如其他答案所说,您缺少break

然而,我强烈建议重构这段代码

例如,将平面编号映射到资源ID。然后在启动时加载:

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);
...

谢谢我想我认为这是一个语法错误,没有休息,所以我没有考虑这个选项。现在我意识到不使用break是一种选择。100%同意最后的评论。苹果公司的Swift语言终于做到了这一点——在C语言出现42年后。我认为C语言仍然需要中断,但会拒绝编译这样的代码。有些语言使用类似Algol的语法Pascal,Ada从一开始就做到了这一点。感谢我一直在寻找一种方法使它变得更简单。我一定会试试的。
plane -= 1;
Picasso.with(context).load(mImageMap.get(plane)).into(image2);
plane_name.setText(plane_names[plane]);