您的位置:首页 > 编程语言 > Java开发

Java数字操作类(Random类)

2018-02-14 10:03 363 查看
public class RandomDemo {
public static void main(String[] args) throws Exception {
// 产生0-99的正整数
Random rand = new Random();
for (int i = 0; i < 10; i++) {
System.out.print(rand.nextInt(100) +",");
}
}
}

import java.util.Random;

public class Demo {
public static void main(String[] args) throws Exception {
Random rand = new Random();
int rBall[] = new int[6];
int bBall[] = new int[1];
int foot = 0;
int count = 0;

while (foot < 6) {
int b = rand.nextInt(34);
if (!isRepeat(rBall, b)) {
rBall[foot++] = b;
}
}
while (count < 1) {
int r = rand.nextInt(16);
if (!isRepeat(bBall, r)) {
bBall[count++] = r;
}
}
java.util.Arrays.sort(rBall);
print(rBall, "红码:");

System.out.println();
print(bBall, "蓝码:");
}

public static boolean isRepeat(int data[], int t) {
if (t == 0) {
return true;
}
for (int i = 0; i < data.length; i++) {
if (data[i] == t) {
return true;
}
}
return false;
}

public static void print(int data[], String str) {
System.out.print(str);
for (int i : data) {
System.out.print(i + " ");
}
}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java random