如何让我的Andr​​oid应用程序产生一个随机数? [英] How do I make my Android app generate a random number?

查看:78
本文介绍了如何让我的Andr​​oid应用程序产生一个随机数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Java,使Android应用程序。你如何使该卷的一些根据用户输入的内容骰子Java程序?

我创建的Java程序只推出1骰子。

你如何获取Java随机摇一到六个?

你如何获得Java的基础上,用户想要的次数,使随机数?

最后,你如何让Java来根据用户输入的数字得出一个形象?

下面我的应用程序是什么样子。

下面是我的code

 包com.warhammerdicerrolleralpha;进口了java.util.Random;
进口android.app.Activity;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.TextView;公共类myMain扩展活动
{
    / **当第一次创建活动调用。
     * @返回* /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);     最后随机myRandom =新的随机(6);    按钮buttonGenerate =(按钮)findViewById(R.id.button1);
    最终的TextView textGenerateNumber =(的TextView)findViewById(R.id.text4);
    buttonGenerate.setOnClickListener(新OnClickListener()
    {           @覆盖
           公共无效的onClick(视图v)
           {
            // TODO自动生成方法存根
            textGenerateNumber.setText(将String.valueOf(myRandom.nextInt()));
           }});    }}

我的xml文件:

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直的android:layout_width =FILL_PARENT
    机器人:填充=5dip机器人:背景=@绘制/ warhammerdicerollalpha
    机器人:layout_height =FILL_PARENT>
    <的TextView的android:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT机器人:文本=@字符串/你好/>
    < EditText上的android:layout_height =WRAP_CONTENT
        机器人:layout_width =match_parent机器人:ID =@ + ID / enternumberofdice>< /&的EditText GT;
    <按钮的android:文本=按钮机器人:ID =@ + ID /按钮1
        机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT>< /按钮>
    <的TextView的android:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT机器人:ID =@ + ID / generatenumber/>
    <的TextView的android:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT机器人:文本=LOL机器人:ID =@ + ID /文本4>< / TextView的>< / LinearLayout中>


解决方案

如何<一个href=\"http://developer.android.com/reference/java/util/Random.html\"><$c$c>Random.nextInt()?

用法:

 最后随机兰特=新的随机();
INT diceRoll = rand.nextInt(6)+ 1; //均匀地从1至6个分布的int

在更新的形象方面,有你想要显示(大小相同)并使用开关不同的<$ C $之间进行选择6种不同的图像C> myImageView.setImageResource(R.drawable.dice_n)。

I'm new to Java and making android applications. How do you make a Java program that rolls a number of dice based on what the user entered?

The Java program I created only rolls one dice.

How do you get Java to roll randomly from one to six?

How do you get Java to make random numbers based on the number of times the user wants?

Lastly, how do you get Java to draw a image based on the number the user entered?

Here what my application looks like.

.

Here's my code

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()));
           }});

    }

}

My xml file:

<?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>

解决方案

How about Random.nextInt()?

Usage:

final Random rand = new Random();
int diceRoll = rand.nextInt(6) + 1; // uniformly distributed int from 1 to 6

In terms of updating an image, have the 6 different images you want to display (all the same size) and use a switch to select between different myImageView.setImageResource(R.drawable.dice_n).

这篇关于如何让我的Andr​​oid应用程序产生一个随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆