【问题标题】:Method isn't pulling the correct variable from an Array方法没有从数组中提取正确的变量
【发布时间】:2014-01-30 03:45:50
【问题描述】:

这应该是一个骰子应用程序,每个骰子应该在 3 种颜色之间交替,所以每个面值可以是蓝色、黄色或红色,到目前为止,我只制作了第一个有颜色的骰子。现在我有一个数组存储用于每种颜色的所有图像,并且数组中有 18 个值,因为骰子有 6 面,每面有 3 种颜色。所以我的方法在数组中取 0-2,在红蓝和黄色中应该都是 1,然后 2-5 在红蓝黄色中应该是 2。问题是每当我运行它时,它显示的总数永远不是正确的总数,因为面值总是关闭。例如,它将显示红色 3 和正常的黑白 1,总数为 8。任何帮助将不胜感激!

public class MainActivity extends Activity {

int[] images = new int[] {

        // dicex1
        R.drawable.dicex1red,
        R.drawable.dicex1blue,
        R.drawable.dicex1yellow,

        // dicex2
        R.drawable.dicex2red,
        R.drawable.dicex2blue,
        R.drawable.dicex2yellow,

        // dicex3
        R.drawable.dicex3red,
        R.drawable.dicex3blue,
        R.drawable.dicex3yellow,

        // dicex4
        R.drawable.dicex4red,
        R.drawable.dicex4blue,
        R.drawable.dicex4yellow,

        // dicex5
        R.drawable.dicex5red,
        R.drawable.dicex5blue,
        R.drawable.dicex5yellow,

        // dicex6
        R.drawable.dicex6red, 
        R.drawable.dicex6blue,
        R.drawable.dicex6yellow, };

Random three = new Random();

MediaPlayer mp;
ImageView dice1, dice2;
TextView total;
Button roll;
int di1, di2, num;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    roll = (Button) findViewById(R.id.bRoll);
    total = (TextView) findViewById(R.id.tvTotal);
    dice1 = (ImageView) findViewById(R.id.IVdice1);
    dice2 = (ImageView) findViewById(R.id.IVdice2);
    mp = new MediaPlayer();
    final Random random = new Random();

    roll.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            mp.release();
            di1 = random.nextInt(6);
            di1++;
            di2 = random.nextInt(6);
            di2++;

            if (di1 == 1) {
                numb(2);

                numColor();
            } else if (di1 == 2) {
                numb(5); 
                num = num + 3;
                numColor();
            } else if (di1 == 3) {
                numb(8); 
                num = num + 6;
                numColor();
            } else if (di1 == 4) {
                numb(11); 
                num = num + 9;
                numColor();
            } else if (di1 == 5) {
                numb(14); 
                num = num + 12;
                numColor();
            } else if (di1 == 6) {
                numb(17); 
                num = num + 15 ;
                numColor();
            }

            if (di2 == 1) {
                dice2.setImageResource(R.drawable.dicex1);
            } else if (di2 == 2) {
                dice2.setImageResource(R.drawable.dicex2);
            } else if (di2 == 3) {
                dice2.setImageResource(R.drawable.dicex3);
            } else if (di2 == 4) {
                dice2.setImageResource(R.drawable.dicex4);
            } else if (di2 == 5) {
                dice2.setImageResource(R.drawable.dicex5);
            } else if (di2 == 6) {
                dice2.setImageResource(R.drawable.dicex6);
            }
            int answer = di1 + di2;
            total.setText("The total is " + answer);

            mp = MediaPlayer.create(getApplicationContext(),
R.raw.dice);
            mp.start();

        }

    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void numColor() {



    if (num == 1) {
        dice1.setImageResource(images[0]);
    } else if (num == 2) {
        dice1.setImageResource(images[1]);
    } else if (num == 3) {
        dice1.setImageResource(images[2]);
    } else if (num == 4) {
        dice1.setImageResource(images[3]);
    } else if (num == 6) {
        dice1.setImageResource(images[4]);
    } else if (num == 7) {
        dice1.setImageResource(images[5]);
    } else if (num == 8) {
        dice1.setImageResource(images[6]);
    } else if (num == 9) {
        dice1.setImageResource(images[7]);
    } else if (num == 10) {
        dice1.setImageResource(images[8]);
    } else if (num == 11) {
        dice1.setImageResource(images[11]);
    } else if (num == 12) {
        dice1.setImageResource(images[12]);
    } else if (num == 13) {
        dice1.setImageResource(images[13]);
    } else if (num == 14) {
        dice1.setImageResource(images[14]);
    } else if (num == 15) {
        dice1.setImageResource(images[15]);
    } else if (num == 16) {
        dice1.setImageResource(images[16]);
    } else if (num == 17) {
        dice1.setImageResource(images[17]);
    } else if (num == 18) {
        dice1.setImageResource(images[18]);
    } 
}

public void numb(int x){
    num = three.nextInt(x);
}
}

【问题讨论】:

  • 我很难理解。如我错了请纠正我。您想找出正确的可绘制给定颜色和值。是这样吗?
  • 是的,我希望它始终随机选择 3 种颜色中的 1 种颜色作为骰子上的数字,但它不会从数组中选择正确的可绘制对象或其他东西,因为 2 个骰子的总数永远不会正确。

标签: java android arrays methods


【解决方案1】:

这就是我会做的事情

private static final int RED = 0;
private static final int BLUE = 1;
private static final int YELLOW = 2;

private int getResourceForDie(int color, int value) {
    return images[3*(value - 1) + color];
}

所以要获得价值 3 的黄色骰子的资源,您可以调用 getResourceForDie(YELLOW, 3),其中价值介于 1 和 6 之间。

【讨论】:

  • 这看起来可以工作,但我对 android 还很陌生,对如何在我的应用程序中实现这一点有点困惑。
  • 如果你想让 dice1 成为一个黄色骰子,价值为 3,你打电话给dice1.setImageResource(getResourceForDie(YELLOW, 3));
  • 好的,所以我让我的数组存储所有图像,然后添加这个方法。我将如何使用 Random() 从该方法中调用随机数和颜色?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
相关资源
最近更新 更多