这是java SE中随机函数的示例,随机生成双色球
代码如下:
/**随机双色球程序**/
package com.itjob.shuangseqiu;
import java.util.Arrays;
import java.util.Random;
public class ShuangSeQiu {
public static void main(String[] args) {
int[] redBall = new int[6];
int blueBall = 0;
Random rand = new Random();
//1个篮球1~16
blueBall = rand.nextInt(16) + 1;
//6个红球 1~33,并且不能重复
for (int i = 0; i < redBall.length; i++) {
redBall[i] = rand.nextInt(33) + 1;
for (int j = 0; j < i; j++) {
if (redBall[j] == redBall[i]) {//判断重复
i--;
break;
}
}
}
Arrays.sort(redBall);//排序
//输出,当小于10的时候,输出"0" + redBall
System.out.println("随机产生的6个红球为");
for(int i = 0; i < 6; i++){
if (redBall[i] < 10) {
System.out.print("0" + redBall[i] + " ");
}else {
System.out.print(redBall[i] + " ");
}
}
System.out.println("\n随机产生的6个红球为");
if (blueBall < 10) {
System.out.print("0" + blueBall + " ");
}else {
System.out.print(blueBall + " ");
}
}
}
感谢您的阅读,欢迎参观我的个人网站:小嗨词典【 https://www.happydict.cn】