在android中返回随机值

时间:2013-12-17 21:28:52

标签: android random

我有一个按钮,要求运行此功能。它应该返回1到6之间的随机值,并且应该为该值分配图片。我现在写了它,以便每个值返回相同的图像。我希望它是这样的,如果值为1,它将返回图像A,如果值为6,它将返回图像B,如果值为2-5,它将返回图像C.我不确定如何实现多值。我认为一个案例可行,但它似乎没有回答我的问题。如果你能指出我正确的方向,我会很感激。

protected void rollPlanechaseDice() {
    int i = 0;
    switch (this.mRandom.nextInt(6)) {
    default:
        i = R.drawable.planeswalker_dice;
    case 0:
    case 1:
    }
    while (true) {
        ImageView localImageView = new ImageView(this);
        localImageView.setImageResource(i);
        new AlertDialog.Builder(this)
                .setView(localImageView)
                .setTitle(R.string.planechase_dice)
                .setPositiveButton(getText(17039370),
                        new 
DialogInterface.OnClickListener() {
                            public void onClick(

DialogInterface paramAnonymousDialogInterface,
                                    int 
paramAnonymousInt) {

paramAnonymousDialogInterface.dismiss();
                            }
                        }).create().show();

        i = R.drawable.planeswalker_dice_chaos;
        return;

1 个答案:

答案 0 :(得分:2)

random.nextInt(6)将返回0到5之间的随机数(从不6)

int i = 0;
switch (this.mRandom.nextInt(6)) {
case 0:
    i = R.drawable.planeswalker_plate;
    break;
case 5:
    i = R.drawable.planeswalker_spoon;
    break;
default:
    i = R.drawable.planeswalker_dice;
    break;
}