我正在制作一个扫雷游戏,而且我在更新捆绑中的值时遇到了问题。首先我将难度值设置为0,然后使用难度值为游戏选择其他值并将其放入捆绑中。这是它的外观:
difficulty 0
rows 9
columns 9
mines 10
然后我决定增加难度,但游戏的值不会更新:
difficulty 1
rows 9
columns 9
mines 10
当我选择新的难度时,为什么捆绑的值不会改变?源代码发布在下面。
MainActivity.java:
public class MainActivity extends Activity {
private int difficulty;
//...
private void startGame(){
Bundle b = new Bundle(3);
Log.d("difficulty",""+difficulty);
switch(difficulty){
case 1:{
b.putInt("rows",16);
b.putInt("columns",16);
b.putInt("mines",40);
}
case 2:{
b.putInt("rows",30);
b.putInt("columns",16);
b.putInt("mines",99);
}
default:{
b.putInt("rows",9);
b.putInt("columns",9);
b.putInt("mines",10);
}
}
gridFragment = new GridFragment();
gridFragment.setArguments(b);
getFragmentManager().beginTransaction().add(R.id.fragment_container, gridFragment,"gridFragment").commit();
}
//...
}
GridFragment.java:
public class GridFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_grid, container, false);
Log.d("rows",getArguments().getInt("rows"));
Log.d("columns",getArguments().getInt("columns"));
Log.d("mines",getArguments().getInt("mines"));
return view;
}
//...
}
答案 0 :(得分:2)
你需要在每个案例后加break
。否则它将进入下一个案例,依此类推。在您的情况下,所有情况都以默认结束。
switch (difficulty) {
case 1:
...
break;
case 2:
...
break;
}