0

我正在制作一款扫雷游戏,但在更新捆绑包中的值时遇到问题。首先,我将难度值设置为 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;
    }
    //...
}
4

1 回答 1

2

you need to put break after each case. Otherwise it falls through to the next case and so on. In your case all cases ends with default.

switch (difficulty) {
   case 1:
     ...
     break;
   case 2:
     ...
     break;
}
于 2015-02-03T23:26:51.887 回答