新手求教育,求解答。在线等

社会现实 2012-01-28 05:35:12
1, 彩票生成系统:
生成一组随机的双色球彩票(一共7个数字,前六位随机范围为1-33,最后一位范围1-16),使用数组保存生成的每次随机数。要求数组的前六位不能出现重复的数字。提示:使用数组和二重循环。
...全文
200 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
guosen_it_nj 2012-02-23
  • 打赏
  • 举报
回复
int[] ary = new int[7];
int begin = 0;
while (true) {
boolean result = false;
int num = (int) (Math.random() * 33 + 1);

// 判断数组里是否存在生成的随机数 num
for (int j = 0; j < ary.length - 1; j++) {
if (ary[j] == num) {
result = true;
break;
}
}
// 如果不存在的话进行添加新的元素数据
if (!result) {
// 前六位
ary[begin] = num;
begin++;
if (begin >= 6) {
num = (int) (Math.random() * 16 + 1);
// 后一位
ary[begin] = num;
break;
}
}
}


for (int i = 0; i < ary.length; i++) {
System.out.println(ary[i]);
}
stevenzxl 2012-02-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 m8225187 的回复:]

大哥 随机数我是弄出来了。。但是我就是不知道怎么把重复的数字去掉 换一个新的整数进去。。这是唯一让我头疼的事
[/Quote]
你可以用set存储,循环的次数是set.size()<7这样就不用担心重复数字了
coolws123 2012-02-06
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ldh911 的回复:]
看你也是真心自己在整,直接给你代码吧:


Java code


// 初始化抽奖
int[] result = new int[7];
Random rand = new Random();

// 初始化号码池
ArrayList<Integer> lst = new ArrayList<I……
[/Quote]

根据你的思路,利用 Collections.shuffle(lst); 亂數排序便完成,然后循环取前6个
coolws123 2012-02-06
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 shuaige112500 的回复:]
Random random = new random()java自己带的Random()方法吗 nextInt是什么意思呢
[/Quote]

nextInt表示生成的是int型
shuaige112500 2012-02-02
  • 打赏
  • 举报
回复
Random random = new random()java自己带的Random()方法吗 nextInt是什么意思呢
Mourinho 2012-02-01
  • 打赏
  • 举报
回复

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

/*
* 生成一组随机的双色球彩票(一共7个数字,前六位随机范围为1-33,最后一位范围1-16),
* 使用数组保存生成的每次随机数。要求数组的前六位不能出现重复的数字。
* 提示:使用数组和二重循环。
*/

/*
* 思路:
* 1.生成1-33,随机打乱顺序获取前6位
* 2.最后1位用随机数实现
*/
public class Test {

public static void main(String[] args) {
System.out.println(Arrays.toString(getNumbers()));
}

static int[] getNumbers(){
int[] result = new int[7];//彩票号码
int[] arrays = new int[33];//1-33
for(int i = 0;i < arrays.length;i++)
arrays[i] = i + 1;
//随机打乱顺序
Random random = new Random();
for(int i = 0;i < arrays.length;i++){
int seed = random.nextInt(arrays.length);//下标为0-32
int temp = arrays[seed];
arrays[seed] = arrays[i];
arrays[i] = temp;
}
//设置彩票
for(int i = 0;i < result.length - 1;i++){
//设置前6位
result[i] = arrays[i];
}
//设置最后1位
result[result.length - 1] = random.nextInt(16) + 1;//获取1-16随机数
return result;
}
}

MiceRice 2012-01-28
  • 打赏
  • 举报
回复
谈不上面向对象,只是奖池的机制不同,纯用数组的机制也可以实现的。不过客观地说,你的机制不是太好,如果涉及到抽取数量较多,可能浪费掉的重复抽取次数就多了。


// 初始化抽奖
int[] result = new int[7];
Random rand = new Random();

// 初始化号码池
int[] pool = new int[33];
for (int i = 0; i < pool.length; i++) {
pool[i] = i + 1;
}

// 摇号:前6个
for (int i = 0; i < 6; i++) {
int pos = rand.nextInt(33 - i);
result[i] = pool[pos];
pool[pos] = pool[33 - 1 - i]; // 将奖池的最后一个数字置换掉当前的;
}

result[6] = rand.nextInt(16) + 1;

System.out.println(Arrays.toString(result));
社会现实 2012-01-28
  • 打赏
  • 举报
回复
谢谢大神。。。。不过你的方法 我看不懂(我刚学) 你的方法应该是 面相对象编程的吧???
但还是谢谢你。。。我的思路被你说同了 呵呵


我刚学的是这样的代码

public class five {
public static void main(String[] args){
int[] qianliu = new int [7];
for(int i=0;i<6;i++){
qianliu[i]= (int)(Math.random()*33+1);
for(int j=0;j<i;j++){
if(qianliu[j]==qianliu[i]){
i--;
break;
}
}
qianliu[6]= (int)(Math.random()*33+1);
}
for(int i=0;i<qianliu.length;i++){
System.out.print(qianliu[i]+" ");
}
}

}
MiceRice 2012-01-28
  • 打赏
  • 举报
回复
看你也是真心自己在整,直接给你代码吧:


// 初始化抽奖
int[] result = new int[7];
Random rand = new Random();

// 初始化号码池
ArrayList<Integer> lst = new ArrayList<Integer>();
for (int i = 1; i <= 33; i++) {
lst.add(i);
}

// 摇号:前6个
for (int i = 0; i < 6; i++) {
int pos = rand.nextInt(lst.size());
int num = lst.remove(pos);
result[i] = num;
}

result[6] = rand.nextInt(16) + 1;

System.out.println(Arrays.toString(result));


输出:
[19, 7, 23, 13, 33, 28, 3]
社会现实 2012-01-28
  • 打赏
  • 举报
回复
大哥 随机数我是弄出来了。。但是我就是不知道怎么把重复的数字去掉 换一个新的整数进去。。这是唯一让我头疼的事
MiceRice 2012-01-28
  • 打赏
  • 举报
回复
这个不复杂吧?你是想求算法还是想求代码?如果是算法的话,给个不算太高效的:
1、初始化一个数组result,大小为7,用于存放结果;
2、初始化一个List,大小为33,预置好1~33;
3、循环6次,每次随机生成0~List.size()范围的随机数,然后将对应List位置中的数,存入result中,并从List中remove掉;
4、随机生成1~16范围的随机数,存入result[6]。

如果求代码的话,看有没有好心人写给你了。

58,441

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧