I have been working on this yahtzee project and have run into a problem. The dice_number array doesn’t seem to be getting the randomly generated values. The oneScore TextView always displays “–“. I’m posting my code. Thanks in advance for any help given. Also if you need to see anymore of the code let me know.
switch (v.getId()) {
case R.id.rollBtn:
for(i = 0; i < 5; i++){
randnum = random.nextInt(5);
if(i == 0){
images[i].setImageResource(image_array[randnum]);
dice_number[i] = randnum;
}
else if(i == 1){
images[i].setImageResource(image_array[randnum]);
dice_number[i] = randnum;
}
else if(i == 2){
images[i].setImageResource(image_array[randnum]);
dice_number[i] = randnum;
}
else if(i == 3){
images[i].setImageResource(image_array[randnum]);
dice_number[i] = randnum;
}
else if(i == 4){
images[i].setImageResource(image_array[randnum]);
dice_number[i] = randnum;
}
else if(i == 5){
images[i].setImageResource(image_array[randnum]);
dice_number[i] = randnum;
}
}
break;
case R.id.onesBtn:
for (i = 0; i < 5; i++)
{
if (dice_number[i] == 1) {
dice_count[0] += 1;
oneScore.setText(Integer.toString(dice_count[0]));
}
else
oneScore.setText("--");
}
That second for loop re-sets the text for each number. So if you have an array
The
TextViewwill be set to--, then--, then1(ifdice_count[0]was 0), then2, then--, so all you will see is the result fromdice_number[4], which is the last--.You need to either construct a string or use multiple
TextViewsto see the whole array, not just the last element.Also, the if statements in the first for loop aren’t doing anything as far as I can tell.