如何简化不使用if语句,并减少我的代码行数的方法。下面是我的写法:如何简化不使用该方法的if语句
private void RemoveImg() {
while (true) {
rel_with_images.getChildCount();
if ((number_of_Image== 5) {
rel_with_images.removeViewAt(number_of_Image- 1);
sp_1 = sp11.play(sp_1 , 1.0F, 1.0F, 0, 0, 1.0F);
inta11 = sp11.play(intaa, 1.0F, 1.0F, 0, 0, 1.0F);
CheckVibrate();
MainAct c5 = MainAct .this;
c5.number_of_Image = (-1 + c5.number_of_Image);
break;
}
if ((number_of_Image== 4) {
rel_with_images.removeViewAt(number_of_Image- 1);
sp_1 = sp11.play(sp_1 , 1.0F, 1.0F, 0, 0, 1.0F);
inta11 = sp11.play(intaa, 1.0F, 1.0F, 0, 0, 1.0F);
CheckVibrate();
MainAct c5 = MainAct .this;
c5.number_of_Image = (-1 + c5.number_of_Image);
break;
}
if ((number_of_Image== 3) {
rel_with_images.removeViewAt(number_of_Image- 1);
sp_1 = sp11.play(sp_1 , 1.0F, 1.0F, 0, 0, 1.0F);
inta11 = sp11.play(intaa, 1.0F, 1.0F, 0, 0, 1.0F);
CheckVibrate();
MainAct c5 = MainAct .this;
c5.number_of_Image = (-1 + c5.number_of_Image);
break;
}
if ((number_of_Image== 2) {
rel_with_images.removeViewAt(number_of_Image- 1);
sp_1 = sp11.play(sp_1 , 1.0F, 1.0F, 0, 0, 1.0F);
inta11 = sp11.play(intaa, 1.0F, 1.0F, 0, 0, 1.0F);
CheckVibrate();
MainAct c5 = MainAct .this;
c5.number_of_Image = (-1 + c5.number_of_Image);
break;
}
if ((number_of_Image== 1) {
rel_with_images.removeViewAt(number_of_Image- 1);
sp_1 = sp11.play(sp_1 , 1.0F, 1.0F, 0, 0, 1.0F);
inta11 = sp11.play(intaa, 1.0F, 1.0F, 0, 0, 1.0F);
CheckVibrate();
MainAct c5 = MainAct .this;
c5.number_of_Image = (-1 + c5.number_of_Image);
break;
}
}
}
number_of_Image是Integer,它用相对布局rel_with_images来计数图像的数量。当布局中有超过30张图片时,代码有点大。有谁知道解决方案?由于
为什么你的if语句开始吗?据我所知,每个if语句中的代码都是相同的。毫无意义的陈述(除非我读错了),只要删除它。 – apmartin1991 2014-10-08 15:57:44