int s = -1;
int a[] = new int[100];
int i = 0;
while (i < 100) {
s = (int) (Mathrandom() 100 + 1);
for (int j = 0; j < 100; j++) {
if (a[j] == s) {
break;
} else if (j == i) {
a[i] = s;
i++;
break;
}
}
}
}int number = new Random()nextInt(5);
new Random()nextInt(5)产生的是0-4的随机数
new Random()nextInt(4)产生的是0-3的随机数
采纳下我哦,谢谢
随机数控制在某个范围内,使用模数运算符%\x0d\ import javautil;\x0d\ class TestRandom {\x0d\ public static void main(String[] args) {\x0d\ Random random = new Random();\x0d\ for(int i = 0; i 回答于 2022-11-16public class Test {public static void main(String[] args) {
for(int i = 0; i < 4; i++){
Systemoutprintln("第" + (i + 1) + "位随机数为:" + (int)(Mathrandom()9000+1000));
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)