这应该是一个骰子应用程序,每个骰子应该在3种颜色之间交替,所以每个面值可以是蓝色,黄色或红色,到目前为止我只是使第一个骰子着色。现在我有一个数组存储用于每种颜色的所有图像,数组中有18个值,因为骰子有6个边,每边有3个颜色。所以我的方法在数组中取0-2,红色蓝色和黄色都应该是1,然后2-5应该是红蓝黄色的2。问题是每当我运行它时,它显示的总数永远不是正确的toal,因为面值总是关闭。例如,它将显示红色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);
}
}
答案 0 :(得分:0)
这就是我要做的事情
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之间。