public class User {
public static void main(String[] args) {
int[] n=new int[6];
for (int i = 0; i < n.length; i++) {
int in=(int)((Math.random()*33)+6);
System.out.println(in);
for (int j = 0; j < n.length; j++) {
if(in!=n[j]){
n[j]=in;
//我要判断出6个不同的随机数放进数组,哪里出问题了?
System.out.println(n[j]);
}
continue;
}
}
}
}
public class TestMath {
/**
* @param args
*/
public static void main(String[] args) {
int [] a=new int [6];
TestMath tm=new TestMath();
for(int i=1;i>0;i++){
if(0!=a[5]){
break;
}
int c=tm.getRandom();
int cunm=tm.checkNum(a, c);
if(1==cunm){
for (int j = 0; j < a.length; j++) {
//创建int数组 默认每个坐标上的值是为0;
if(0==a[j]){
a[j]=c;
break;
}
}
}
}
//排序
Arrays.sort(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+",");
}
}
//产生数据数
public int getRandom(){
return (int)((Math.random()*33)+6);
}
//判断该该数组中是不是存在这个随机数
public int checkNum(int[] a,int num){
for (int i = 0; i < a.length; i++) {
if(a[i]!=num){
return 1;
}
}
return -1;
}
}
[code="java"]
public static void main(String[] args) {
int[] n=new int[6];
n[0]=1;
int k = 1;
while(true){
int in=(int)((Math.random()*33)+6);
if(!ArrayUtils.contains(n, in)){
n[k] = in;
k++;
}
if(k >= 6){
break;
}
}
for (int j = 0; j < n.length; j++) {
System.out.println("j:"+n[j]);
}
}
[/code]
首先,楼主在第二次判断时,你可以直接让j从0判断到i就可以了,因为整个数组n中后面的都还没有赋值呢。
另外,可以先写一个一个Map或者List的集合类,把一个生成的随机数放进去,每次放的时候判断Map中是否存在。使用Map或者List的Contains方法。这样更方便些。另外,生成随机数,不知道楼主是为了生成小数还是整数,一般使用Random这个类,new一个出来,直接使用其nextInt()这一类的方法就好了。
public class User {
public static void main(String[] args) {
int[] n=new int[6];
//标志是否已经出现过了
boolean isConsisted = false;
int i = 0;
while(true){
int in=(int)((Math.random()*33)+6);
for (int j = 0; j < i; j++) {
if(n[j] == in){
isConsisted = true;
break;
}
}
if(!isConsisted){
n[i] = in;
i++;
if(i >= n.length){
break;
}
}else{
isConsisted = false;
}
}
}
}
Set set = new HashSet();
for(int i = 0 ;i < 6 ; ){
if(set.add(Math.random()*33+6)){
i++;
}
}
int array = set.toArray();
搞定楼主问题
使用Math.random()来产生随机的几个数,HashSet可以创建一个不重复的集合