如何使我的安卓应用程序生成随机数?

5
我是Java和制作安卓应用的新手。如何编写一个Java程序,根据用户输入的骰子数量来掷骰子?
我创建的Java程序只能掷一次骰子。
如何让Java随机掷出1到6之间的数字?
如何让Java根据用户想要的次数生成随机数字?
最后,如何让Java根据用户输入的数字绘制图像?
这是我的应用程序的外观。

Valid XHTML .

这是我的代码

package com.warhammerdicerrolleralpha;

import java.util.Random;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class myMain extends Activity 
{
    /** Called when the activity is first created. 
     * @return */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);



     final Random myRandom = new Random(6);



    Button buttonGenerate = (Button)findViewById(R.id.button1);
    final TextView textGenerateNumber = (TextView)findViewById(R.id.text4);




    buttonGenerate.setOnClickListener(new OnClickListener()
    {

           @Override
           public void onClick(View v) 
           {
            // TODO Auto-generated method stub
            textGenerateNumber.setText(String.valueOf(myRandom.nextInt()));
           }});

    }

}

我的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:padding="5dip" android:background="@drawable/warhammerdicerollalpha"
    android:layout_height="fill_parent">


    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/hello" />
    <EditText android:layout_height="wrap_content"
        android:layout_width="match_parent" android:id="@+id/enternumberofdice"></EditText>
    <Button android:text="Button" android:id="@+id/button1"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:id="@+id/generatenumber" />
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="LOL" android:id="@+id/text4"></TextView>

</LinearLayout>

4
如果今天是星期五,我会重新给这个问题贴上“空间海军陆战队”的标签。 - Rob Hruska
“roll one dice” 是什么意思?我这里没有看到骰子。 - gigadot
我正在尝试让我的骰子在一到六之间随机掷出一个数字。你知道怎么做吗? - lonesarah
2个回答

14
Random.nextInt()怎么样?
用法:
final Random rand = new Random();
int diceRoll = rand.nextInt(6) + 1; // uniformly distributed int from 1 to 6

更新图片时,先准备好6张你想要显示的不同图片(尺寸相同),然后使用switch语句在不同的myImageView.setImageResource(R.drawable.dice_n)之间进行选择。


我得到了一个错误:在不同方法中定义的内部类中无法引用非最终变量myRandom。 - lonesarah
nextInt(5) 会生成从0(包括)到5(不包括)的随机数。因此,您需要使用 nextInt(6)+1。 - Maaalte
@lonesarah 然后将其声明为 final - 在 Random 之前添加 final 关键字,就像您的代码中已经有的那样。 - fredley
好的,现在Eclipse想要删除diceRoll,因为局部变量diceRoll从未被读取。 - lonesarah

1

使用带有long种子的Random对象(System.currentTimeMillis()是一个好选择)。然后从您的对象调用nextInt(int n)方法并传递骰子大小。(请记住,nextInt(int n)的范围从0开始,因此您将要传入骰子大小,然后将1添加到结果中)。

使用long种子的原因是为了提高数字分布的(伪)随机性。

如果您还没有声明骰子大小为常量,则应该这样做。关于Random的Javadoc 在这里


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接