如何生成一个随机数,然后显示在屏幕上? [英] How to generate a random number, then display it on screen?

查看:1983
本文介绍了如何生成一个随机数,然后显示在屏幕上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,即时通讯相当新的Andr​​oid,但我已成功地教我的基础,我想提出,你preSS一个按钮,一个新的屏幕打开一个应用程序,它显示了一个随机生成的数字,唯一的问题是我不知道如何来生成并显示随机数,我一直在寻找的年龄网页和只发现了很少的信息片段,这dosent真正意义的我。 :/

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. :/

如果有人可以帮助我,甚至给我的信息的只是一点点应指导我在正确的方向将是巨大的。

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

编辑:(对下面的评论)

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

多数民众赞成在code,我有,我在哪里出了错^^^^:/

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

推荐答案

Android的文档非常出色。下面是一个Hello World应用程序:

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

<一个href=\"http://developer.android.com/guide/tutorials/hello-world.html\">http://developer.android.com/guide/tutorials/hello-world.html

只要修改

tv.setText("Hello, Android");

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

和确保导入数学库(如果你使用Eclipse,按Ctrl + Shift + O)。

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

这篇关于如何生成一个随机数,然后显示在屏幕上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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