How can I create an app that generates a random number in Android using Eclipse and then show the result in a TextView
field? The random number has to be in a range selected by the user. So, the user will input the max and min of the range, and then I will output the answer.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
So you would want the following:
...somewhere in your code put the method to get the min and max from the user when they click submit and then use them in the following line of code:
This will set random to a random number between the user selected min and max. Then you will do:
You can use If
Random
. For example, this generates a random number between 75 to 100.Also, from API level 21 this is possible:
This gives a random integer between 28 (inclusive) and 45 (exclusive), one of 28,29,...,43,44.
To extend what Rahul Gupta said:
You can use the Java function
int random = Random.nextInt(n)
.This returns a random
int
in the range[0, n-1]
.I.e., to get the range
[20, 80]
use:To generalize more: