我已經設置好了,如果返回0,比它上面有1的骰子圖像被設置爲按鈕圖像的位置。它也是用不同的值和圖像來做到這一點。 0 =骰子1,1 =骰子2,2 =骰子3,3 =骰子4,4 =骰子5.問題是所有返回的數字總是0,我知道這是因爲按鈕上都有一個圖像。我看過無數的教程,不知道發生了什麼!請幫我上網的朋友!爲什麼我的隨機發生器總是返回0?
這裏是我的代碼:
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 < DieNumber.length; i++)
{
}
public void numberToImage()
{
for(int i = 0; i < 5; i++)
{
mImageButtons[i].setBackground(mDiceImages[DieNumber[i]]);
}
}
public int nextInt(int n)
{
n = RANDOM.nextInt();
return (n < 0 ? -n : n) % max;
}
http://stackoverflow.com/questions/363681/generating-random-number-in-a-range-with-java – Nimooli 2013-04-29 20:14:00