随机选出双色球的问题

shirui8653719 2013-04-29 07:20:37
package day1;

import java.util.Random;

public class getBall {

public static int[] getDoubleBall(){
int[] polls = {
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
};
int[] ball = new int[7];
boolean[] index = new boolean[20];
Random ran = new Random();
for(int i=0; i<6; i++){
int a = ran.nextInt(20); //a[0-19]
if(index[a] == true){
continue;
}
ball[i] = polls[a];
index[a] = true;
}
ball[6] = polls[ran.nextInt(10)];
return ball;
}

}

package day1;

import java.util.Arrays;

public class DoubleBall {


public static void main(String[] args) {
// TODO Auto-generated method stub
getBall ball = new getBall();
int[] t = ball.getDoubleBall();
System.out.println(Arrays.toString(t));
}
}

选7个球,前6个球在1至20号球里随机选出6个,不能重复,第7个球在1至10号球里选出一个,可以与前6个球有重复,但是跑出来的结果是有0号球,而且有重复,这是为什么?谁能看一下,谢谢。
...全文
204 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
shirui8653719 2013-04-30
  • 打赏
  • 举报
回复
引用 7 楼 shirui8653719 的回复:
[quote=引用 5 楼 lihoujun123 的回复:] 这里有一个更简单的方法
[import java.util.Arrays;
import java.util.Random;


public class DoubleBall {

	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		getBall ball = new getBall();
		int[] t = ball.getDoubleBall();
		System.out.println(Arrays.toString(t));
	}
}
 class getBall {

	public static int[] getDoubleBall(){
		int[] polls = {
				1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
		};
		int[] ball = new int[7];
		Random ran = new Random();
		ball[6] = polls[ran.nextInt(10)];
		for(int i=0; i<6; )
		{
			int a = ran.nextInt(20);         //a[0-19]
			if(polls[a]!=0)
			{
			 ball[i] = polls[a];
			 i++;
			 polls[a]=0;
			}
			
		}
		
		return ball;
	}

}

 /code]
嗯,你这个方法比我那个更简单一点。[/quote][code=java]import java.util.Arrays; import java.util.Random; public class DoubleBall { public static void main(String[] args) { // TODO Auto-generated method stub getBall ball = new getBall(); int[] t = ball.getDoubleBall(); System.out.println(Arrays.toString(t)); } } class getBall{ public static int[] getDoubleBall(){ int[] polls = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 }; int[] ball = new int[7]; Random ran = new Random(); ball[6] = polls[ran.nextInt(10)]; for(int i=0; i<6; ) { int a = ran.nextInt(20); //a[0-19] if(polls[a]!=0) { ball[i] = polls[a]; i++; polls[a]=0; } } return ball; } }
shirui8653719 2013-04-30
  • 打赏
  • 举报
回复
引用 5 楼 lihoujun123 的回复:
这里有一个更简单的方法[code=java][import java.util.Arrays; import java.util.Random; public class DoubleBall { public static void main(String[] args) { // TODO Auto-generated method stub getBall ball = new getBall(); int[] t = ball.getDoubleBall(); System.out.println(Arrays.toString(t)); } } class getBall { public static int[] getDoubleBall(){ int[] polls = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 }; int[] ball = new int[7]; Random ran = new Random(); ball[6] = polls[ran.nextInt(10)]; for(int i=0; i<6; ) { int a = ran.nextInt(20); //a[0-19] if(polls[a]!=0) { ball[i] = polls[a]; i++; polls[a]=0; } } return ball; } } /code]
嗯,你这个方法比我那个更简单一点。
lihoujun123 2013-04-30
  • 打赏
  • 举报
回复
import java.util.Arrays;
import java.util.Random;


public class DoubleBall {

	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		getBall ball = new getBall();
		int[] t = ball.getDoubleBall();
		System.out.println(Arrays.toString(t));
	}
}
 class getBall {

	public static int[] getDoubleBall(){
		int[] polls = {
				1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
		};
		int[] ball = new int[7];
		Random ran = new Random();
		ball[6] = polls[ran.nextInt(10)];
		for(int i=0; i<6; )
		{
			int a = ran.nextInt(20);         //a[0-19]
			if(polls[a]!=0)
			{
			 ball[i] = polls[a];
			 i++;
			 polls[a]=0;
			}
			
		}
		
		return ball;
	}

}

 
lihoujun123 2013-04-30
  • 打赏
  • 举报
回复
这里有一个更简单的方法[code=java][import java.util.Arrays; import java.util.Random; public class DoubleBall { public static void main(String[] args) { // TODO Auto-generated method stub getBall ball = new getBall(); int[] t = ball.getDoubleBall(); System.out.println(Arrays.toString(t)); } } class getBall { public static int[] getDoubleBall(){ int[] polls = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 }; int[] ball = new int[7]; Random ran = new Random(); ball[6] = polls[ran.nextInt(10)]; for(int i=0; i<6; ) { int a = ran.nextInt(20); //a[0-19] if(polls[a]!=0) { ball[i] = polls[a]; i++; polls[a]=0; } } return ball; } } /code]
shirui8653719 2013-04-30
  • 打赏
  • 举报
回复
引用 1 楼 aviyy 的回复:
你的continue那里有问题,for循环6次,遇到重复的continue,boll[i]没有取值,执行下一个循环,不重复就是boll[i+1]取值了,循环只执行6次,没有取值的默认为0 方案:continue;改为i--;continue;就行了
嗯,似乎是这个原因。
三儿样 2013-04-30
  • 打赏
  • 举报
回复
闲着没事写个别的 结帖率:97.83% #8 得分:0 回复于: 2013-04-30 10:35:08 引用 7 楼 shirui8653719 的回复: Quote: 引用 5 楼 lihoujun123 的回复: 这里有一个更简单的方法 Java code?1234567891011121314151617181920212223242526272829303132333435363738394041 [import java.util.Arrays; import java.util.Random; public class DoubleBall { public static void main(String[] args) { // TODO Auto-generated method stub getBall ball = new getBall(); int[] t = ball.getDoubleBall(); System.out.println(Arrays.toString(t)); } } class getBall { public static int[] getDoubleBall(){ int[] polls = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 }; int[] ball = new int[7]; Random ran = new Random(); ball[6] = polls[ran.nextInt(10)]; for(int i=0; i<6; ) { int a = ran.nextInt(20); //a[0-19] if(polls[a]!=0) { ball[i] = polls[a]; i++; polls[a]=0; } } return ball; } } /code] 嗯,你这个方法比我那个更简单一点。[code=java]import java.util.Arrays; import java.util.Random; public class DoubleBall { public static void main(String[] args) { // TODO Auto-generated method stub getBall ball = new getBall(); int[] t = ball.getDoubleBall(); System.out.println(Arrays.toString(t)); } } class getBall{ public static int[] getDoubleBall(){ int[] polls = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 }; int[] ball = new int[7]; Random ran = new Random(); ball[6] = polls[ran.nextInt(10)]; int i=0; do { int a = ran.nextInt(20); //a[0-19] if(polls[a]!=0) { ball[i] = polls[a]; i++; polls[a]=0; } }while(i>5); return ball; } }
huoer_gf 2013-04-29
  • 打赏
  • 举报
回复
引用 1 楼 aviyy 的回复:
你的continue那里有问题,for循环6次,遇到重复的continue,boll[i]没有取值,执行下一个循环,不重复就是boll[i+1]取值了,循环只执行6次,没有取值的默认为0 方案:continue;改为i--;continue;就行了
有道理,想了半天也没想出比这更简单办法!
gemwuu 2013-04-29
  • 打赏
  • 举报
回复
1楼说得对…… 下面也行……
/*选7个球,前6个球在1至20号球里随机选出6个,不能重复。
 * 第7个球在1至10号球里选出一个,可以与前6个球有重复*/
import java.util.Random;

class Ball {
	int poll[];
	Ball(int a[]){
		poll = a;
	}	
}
public class java_1 {
	static int op[] = new int[7];
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
		Ball b = new Ball(a);
		Random ran  = new Random();
		op[0] = b.poll[ran.nextInt(20)];
		for(int i = 1; i< 6; i++) {
			op[i] = b.poll[ran.nextInt(20)];
			for(int j = i-1; j>=0; j--)
				if(op[i]==op[j])
					i--;
				else continue;
		}
		op[6] = b.poll[ran.nextInt(10)];
		for(int i = 0; i<7; i++)
			System.out.print(op[i]+" ");
	}

}
aviyy 2013-04-29
  • 打赏
  • 举报
回复
你的continue那里有问题,for循环6次,遇到重复的continue,boll[i]没有取值,执行下一个循环,不重复就是boll[i+1]取值了,循环只执行6次,没有取值的默认为0 方案:continue;改为i--;continue;就行了

62,621

社区成员

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

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