Java随机一注双色球代码

如果这里输出的号能中奖,那程序员可发达了。

import java.util.Arrays;
import java.util.Random;

public class DoubleBalls {
public static void main(String[] args) {
String[] pools = { "01", "02", "03", "04", "05", "06", "07", "08",
"09", "10", "11", "12", "13", "14", "15", "16", "17", "18",
"19", "20", "21", "22", "23", "24", "25", "26", "27", "28",
"29", "30", "31", "32", "33" };
boolean[] used = new boolean[pools.length];
String[] ball = new String[6];
Random rd = new Random();
int i = 0;
while (true) {
int index = rd.nextInt(pools.length);
if (used[index]) {
continue;
}
ball[i] = pools[index];
used[index] = true;
i++;
if (i == ball.length) {
break;
}
}
Arrays.sort(ball);// 对前6个红球进行排序
ball = Arrays.copyOf(ball, ball.length + 1); // 将ball的数组长度有6变成7
ball[ball.length - 1] = pools[rd.nextInt(16)];// 从pools的数组中前16个元素随机选择一个代表篮球
System.out.println(Arrays.toString(ball));
}
}

转载请注明出处:https://www.onexin.net/the-java-random-a-note-color-ball-code/

相关文章:

1、JAVA扑克牌随即五张(数组实现)
https://www.onexin.net/java-poker-then-five-array-implementation/

2、高性能、高流量Java Web站点打造的22条建议(转)
https://www.onexin.net/high-performance-high-traffic-sites-to-build-java-web-22-recommendations-rpm/

3、Java开源缓存框架介绍
https://www.onexin.net/java-open-source-caching-frameworks-introduced/

4、JAVA程序命名规范
https://www.onexin.net/java-program-naming-conventions/

5、PHP描述冒泡排序和快速排序算法
https://www.onexin.net/php-description-of-bubble-sort-and-quicksort-algorithm/

Leave a Reply