java随机整数(跪求,java随机整数)

本文目录
- 跪求,java随机整数
- java 生成k个50~100之间的随机整数
- JAVA如何生成一个随机整数
- java中如何产生1000个随机的整数
- 在java中随机生成10个整数,用户求输入一个数,判断是否存在于这10个整数中
跪求,java随机整数
int
i
=(int)Math.floor(Math.random()*3);
//得到0-2的整数
注:
Math.random()方法得到随机小数
*4将会得到小于4的数字,floor方法始终向下舍入。
int
i
=(int)Math.ceil(Math.random()*3);
//得到1-3的整数
注:Math.random()方法得到随机小数
*3将会得到小于3的数字,Ceil方法始终向上舍入。
int
i
=(int)Math.round(Math.random()*3);//得到0-3的整数
注:round方法是对与等于或大于0.5向上舍入(四舍五入)
java 生成k个50~100之间的随机整数
实现思路:生成50-100之间的随机数(包括50,包含100),之后循环k次分别输出即可:
int k = 5;
for(int i = 0;i《k;i++){
Random rand = new Random();
int randNum = rand.nextInt(50)+50;
System.out.println(randNum );
}
实现思路:获取到最大数减去最小数的值作为随机生成的数,之后得到的值加上最小数,就是所需要的数值。
JAVA如何生成一个随机整数
public class Test {
//随机生成5个1至100之间的整数。显示这5个数的和以及平均值,然后从大到小显示。
public static void main(String args) {
int;
int pingjun = 0;
for(int i = 0; i 《 arr.length; i++){
arr = new java.util.Random().nextInt(100)+1;
pingjun += arr;
}
java.util.Arrays.sort(arr);
System.out.println("这五个数是:" + java.util.Arrays.toString(arr));
System.out.println("平均的值是:" + (pingjun / 5));
}
}
java中如何产生1000个随机的整数
/**
* 2015年10月23日下午8:37:31
*
* @author cs12110 TODO 完成程序
*/
public class Joke {
/**
* 获取长度为len的一维数组:随机整数
*
* @param len
* 长度
* @return
*/
public int getRandomNumber(int len) {
if (len 《 1) {
len = 1;
}
int;
for (int index = 0; index 《 len; index++) {
arr = (int) (Math.random() * 1000);
}
return arr;
}
/**
* 对数组进行排序
*
* @param arr
* 数组
*/
public void sort(int arr) {
if (null == arr) {
return;
}
for (int index = 0; index 《 arr.length; index++) {
for (int j = 0; j 《 arr.length - index - 1; j++) {
if (arr) {
int temp = arr;
arr;
arr = temp;
}
}
}
}
/**
* 打印数字
*
* @param maxIndex
* 数字最大界限
*/
public void display(int arr) {
for (int index = 0; null != arr && index 《 arr.length; index++) {
if (0 == index % 10) {// 输出10个数字换行输出
System.out.println();
}
System.out.print(arr + " ");
}
}
public static void main(String args) {
Joke joke = new Joke();
joke.goOn("输进任意键,获取1000个随机数 ");
int tempArr = joke.getRandomNumber(1000);
//joke.goOn("输进任意键,打印1000个随机数 ");
//joke.display(tempArr);
joke.goOn("输进任意键,开始对随机数排序");
joke.sort(tempArr);
joke.display(tempArr);
}
}
在java中随机生成10个整数,用户求输入一个数,判断是否存在于这10个整数中
由于你没有指定这10个整数的生成范围,所以我这里假定是0~99之间的整数,这样用户输入时有10%的几率命中。
整体代码为:
public class Main {
public static void main(String args) {
//声明长度为10的随机数数组
int;
Random random = new Random();
for (int i = 0; i 《 10; i++) {
//获取0~99之间的一个随机整数,可通过调整nextInt的参数来修改随机数范围
int num = random.nextInt(100);
//如果新生成的数字已经存在于随机数数组中,则重新生成
if (checkDistinct(randoms, num)) {
i--;
continue;
}
randoms = num;
}
//增序排序,好看
Arrays.sort(randoms);
System.out.println("请输入一个整数:");
Scanner scanner = new Scanner(System.in);
//严谨一点这里其实可以对输入的in进行校验,检验其是不是整数,校验方法很多搜一下就有我这就不校验了
int in = scanner.nextInt();
System.out.println("生成的随机数数组为:");
System.out.println(Arrays.toString(randoms));
if (checkDistinct(randoms, in)) {
System.out.println("输入的数字在其中");
} else {
System.out.println("输入的数字不在其中");
}
}
//检查新生成的数字是否存在于随机数数组中,若存在,返回true
private static boolean checkDistinct(int randoms, int num) {
for (int i = 0; i 《 randoms.length; i++) {
if (randoms == num) {
return true;
}
}
return false;
}
}
运行结果:
输入的数字存在时:
输入的数字不存在时:

更多文章:
arcgis怎么设置文本框样式?用JS怎么实现一个把一个文本框中的选中内容加粗,效果基本上和编辑器的加粗效果相同急需
2026年11月6日 10:50
bios setup(“Bios setup”怎么在电脑里面设置)
2026年11月6日 10:00
php递归删除目录(php怎么删除文件夹和文件夹下的所有文件)
2026年11月6日 03:30
vimoapp安卓手机版(制作VLog常用的APP有哪些呢)
2026年11月6日 01:50




