Android 如何生成一个随机数,然后将其显示在屏幕上?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2405411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:44:02  来源:igfitidea点击:

How to generate a random number, then display it on screen?

androidrandom

提问by Dan

Ok, im fairly new to android but i have managed to teach myself the basics, i am making an app where you press a button , and a new screen opens and it shows a randomly generated number, the only problem is i dont know how to generate and display the random number, i have been searching the web for ages and have only found little snippets of information , that dosent really make sense to me. :/

好的,我对 android 还很陌生,但我已经设法自学了基础知识,我正在制作一个应用程序,你按下一个按钮,一个新屏幕打开并显示一个随机生成的数字,唯一的问题是我不知道如何生成并显示随机数,我在网上搜索了很长时间,只找到了很少的信息片段,这对我来说真的很有意义。:/

If someone could help me , or even give me just a little bit of info that should guide me in the right direction it would be great

如果有人可以帮助我,甚至给我一点信息来指导我朝着正确的方向前进,那就太好了

EDIT: (for the comment below)

编辑:(对于下面的评论)

    super.onCreate(savedInstanceState);
    TextView tv = new TextView(this);
    tv.setText("Random Number : " + Math.random());
    int random = (int)Math.ceil(Math.random()*100);
    setContentView(tv);

Thats the code i have , where have i gone wrong ^^^^ :/

这就是我的代码,我哪里出错了^^^^ :/

回答by Ralphleon

Android's documentation is excellent. Here's a hello world app:

Android 的文档非常好。这是一个 hello world 应用程序:

http://developer.android.com/guide/tutorials/hello-world.html

http://developer.android.com/guide/tutorials/hello-world.html

Just change

只是改变

tv.setText("Hello, Android");

to

tv.setText("Random Number: " + Math.random());

and make sure to import the Math library (if you're using eclipse, hit Ctrl+Shift+O).

并确保导入 Math 库(如果您使用的是 Eclipse,请按 Ctrl+Shift+O)。

回答by duggu

below code return a value in Integer:-

下面的代码以整数形式返回一个值:-

    public static int randomBox() {

    Random rand = new Random();
    int pickedNumber = rand.nextInt(100);
    return pickedNumber;

}

回答by Dan

Random rand = new Random();
String randomInt = list.get(rand.nextInt(list.size()));

it may be help for you

它可能对你有帮助

回答by Shraddha Patel

    Random r = new Random();

    StringBuffer temp = new StringBuffer("Random numbers:");
    for (int i = 0; i < 10; i++) {
        int i1 = r.nextInt(100 - 0) + 0;
        temp.append(String.valueOf(i1));
        temp.append(String.valueOf(" "));
    }
    return temp.toString();

回答by Sephy

Actually, you could easily use :

实际上,您可以轻松使用:

yourVariable = Math.random();

Should work in Android. Gives you a number between 0 and 1. Then you give yourVariable to a TextView with the method .setText(yourVariable) for instance...

应该在安卓上工作。给你一个介于 0 和 1 之间的数字。然后你将 yourVariable 赋给 TextView,例如使用 .setText(yourVariable) 方法......

回答by jqpubliq

Here is your documentation for Random. Beyond that I'm not sure if you want to launch an Activityor update a TextViewor what have you. However, I strongly recommend reading the documentation for Activityas well as common tasks in androidand User Interface. These should help you understand what you are trying to do.

这是您的Random文档。除此之外,我不确定您是否要启动Activity或更新 aTextView或您有什么。但是,我强烈建议您阅读Activity的文档以及androidUser Interface 中的常见任务。这些应该可以帮助您了解您正在尝试做什么。

回答by Nakul Sudhakar

int number = (new Random().nextInt(100));

a random number will be assigned to variable number, each time it is used

每次使用时,都会为变量编号分配一个随机数

回答by Nakul Sudhakar

If you are willing to devloper your own algorithms to generate random number, pick any algo and implement in any language of preference. https://en.wikipedia.org/wiki/List_of_random_number_generators

如果您愿意开发自己的算法来生成随机数,请选择任何算法并以任何偏好的语言实现。https://en.wikipedia.org/wiki/List_of_random_number_generators

回答by Agilanbu

Different way, but simple:)

不同的方式,但很简单:)

    Calendar c = Calendar.getInstance();
    final int seconds = c.get(Calendar.SECOND);

    Random r = new Random();
    final int n = r.nextInt(80 - 65) + 65;

    Toast.makeText(getApplicationContext(), ""+ n + seconds, Toast.LENGTH_SHORT).show();  

回答by Hardip

        Random r = new Random();
        rendomNumber = r.nextInt(80- 65) + 65;
        System.out.print(""+rendomNumber )