Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does my random generator always return 0?
    primarykey
    data
    text
    <p>I have it set up so If a 0 is returned, than an image of a dice with a 1 on it is set to the place of a buttons image. It does this with different values and images too. 0 = Dice 1, 1 = Dice 2, 2 = Dice 3, 3 = Dice 4, 4 = Dice 5. The problem is that all numbers returned are always 0, I know this because the buttons all have a one image on them. I have looked at countless tutorials and have no idea what is going on! Please help me internet friends!</p> <p>Here is my code:</p> <pre><code>package com.example.yahtzee; import java.util.Random; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.ImageButton; public class MainActivity extends Activity { static int mButtonAmount = 7; static int SidesOfDice = 6; final static int NumberOfDice = 6; int Rolls = 1; boolean mCACV = true; String dialog_title; String dialog_message; String positive_button; String neutral_button; String negitive_button; int DiceNumber; int placeHolder; int max = NumberOfDice + 1; Button[]mButtons = new Button[mButtonAmount]; Boolean[]mButtonBools = new Boolean[mButtonAmount]; Drawable[]mDiceImages = new Drawable[SidesOfDice]; Boolean[]mIsDiceHeld = new Boolean[NumberOfDice]; ImageButton[]mImageButtons = new ImageButton[NumberOfDice]; int[]DieNumber = new int[NumberOfDice]; private static final Random RANDOM = new Random(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButtons[0] = (Button) findViewById(R.id.start_roll); mButtons[0].setEnabled(true); mButtons[1] = (Button) findViewById(R.id.stop_roll); mButtons[1].setEnabled(false); mButtonBools[0] = getResources().getBoolean(R.bool.is_start_roll_button_pressable); mButtonBools[1] = getResources().getBoolean(R.bool.is_stop_roll_button_pressable); mDiceImages[0] = getResources().getDrawable(R.drawable.diceside1); mDiceImages[1] = getResources().getDrawable(R.drawable.diceside2); mDiceImages[2] = getResources().getDrawable(R.drawable.diceside3); mDiceImages[3] = getResources().getDrawable(R.drawable.diceside4); mDiceImages[4] = getResources().getDrawable(R.drawable.diceside5); mDiceImages[5] = getResources().getDrawable(R.drawable.diceside6); mImageButtons[0] = (ImageButton) findViewById(R.id.die_1); mImageButtons[0].setClickable(false); mImageButtons[1] = (ImageButton) findViewById(R.id.die_2); mImageButtons[1].setClickable(false); mImageButtons[2] = (ImageButton) findViewById(R.id.die_3); mImageButtons[2].setClickable(false); mImageButtons[3] = (ImageButton) findViewById(R.id.die_4); mImageButtons[3].setClickable(false); mImageButtons[4] = (ImageButton) findViewById(R.id.die_5); mImageButtons[4].setClickable(false); mIsDiceHeld[0] = false; mIsDiceHeld[1] = false; mIsDiceHeld[2] = false; mIsDiceHeld[3] = false; mIsDiceHeld[4] = false; controlVariables(); } public void controlVariables() { rollDice(); numberToImage(); } public void checkAndChangeButtons() { checkStartButton(); checkStopButton(); } public void rollDice() { for(int i = 0; i &lt; DieNumber.length; i++) { } public void numberToImage() { for(int i = 0; i &lt; 5; i++) { mImageButtons[i].setBackground(mDiceImages[DieNumber[i]]); } } public int nextInt(int n) { n = RANDOM.nextInt(); return (n &lt; 0 ? -n : n) % max; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload