public String GetRndNum(int c){
int[] num=new int[c];
int numtemp;
int re;
Random rnd=new Random();
StringBuffer sb=new StringBuffer();
for(int i=0;i<num.length;i++)
{ do{numtemp=rnd.nextInt(100);
re=0;
for(int m=0;m<i;m++)
{if(num[m]==numtemp)
re++;
}//for
}while(re!=0);
num[i]=numtemp;
}//for
for(int i=0;i<num.length;i++)
{if(i==num.length-1)
sb.append(num[i]);
else
sb.append(num[i]+",");
}//for
return sb.toString();
}
}
int[] num=new int[c];
int numtemp;
int re;
Random rnd=new Random();
StringBuffer sb=new StringBuffer();
for(int i=0;i<num.length;i++)
{ do{numtemp=rnd.nextInt(100);
re=0;
for(int m=0;m<i;m++)
{if(num[m]==numtemp)
re++;
}//for
}while(re!=0);
num[i]=numtemp;
}//for
for(int i=0;i<num.length;i++)
{if(i==num.length-1)
sb.append(num[i]);
else
sb.append(num[i]+",");
}//for
return sb.toString();
}
}
博客给出一段Java代码,定义了GetRndNum方法,用于生成指定长度的随机数字字符串。方法中使用Random类生成随机数,通过循环和条件判断确保数字不重复,最后将数字拼接成字符串返回。
2万+

被折叠的 条评论
为什么被折叠?



