程序代码如下:想实现checkebox_s数组和s数组的内容的随机组合并放到f数组当中,可是如果我的s数组人数小于3人时候则会出现bug,程序被中断,而且奇怪的是我的checkbox的信息被保存了下来,即使我没有选中该复选框,也能够得到该复选框的内容,求高手解救
String[] s=new String[100];
String[] checkebox_s=new String[100];
String[] g=new String[100];
String[] f=new String[100];
for(int i=0;i<=info.length();i++)
{
s=info.split(",");
}
for(int j=0;j<=u5.length();j++)
{
checkebox_s=u5.split(",");
}
String r=null;
Random ran=new Random();
int index;//=(int)ran.nextInt(s.length);
try{for(int d=0;d< f.length;d++)
{
index=(int)ran.nextInt(s.length);
r=s[index];
this.show4.append(r) ;
for(int k=0;k<checkebox_s.length;k++)
{
if(g[k]==r )
{
k--;
//d--;
break;
}else if(k==d )
{
g[k]=r;
if(checkebox_s[k]==null)
{
checkebox_s[k]="睡觉休息";
}
f[k]= "判处 "+g[k]+" 负责 "+checkebox_s[k];
this.show3.append(f[k]) ;
//this.show4.append(f[k]) ;
break;
}
}
}
}catch( Exception e)
{
e.printStackTrace(); }
String[] s=new String[100];
String[] checkebox_s=new String[100];
String[] g=new String[100];
String[] f=new String[100];
for(int i=0;i<=info.length();i++)
{
s=info.split(",");
}
for(int j=0;j<=u5.length();j++)
{
checkebox_s=u5.split(",");
}
String r=null;
Random ran=new Random();
int index;//=(int)ran.nextInt(s.length);
try{for(int d=0;d< f.length;d++)
{
index=(int)ran.nextInt(s.length);
r=s[index];
this.show4.append(r) ;
for(int k=0;k<checkebox_s.length;k++)
{
if(g[k]==r )
{
k--;
//d--;
break;
}else if(k==d )
{
g[k]=r;
if(checkebox_s[k]==null)
{
checkebox_s[k]="睡觉休息";
}
f[k]= "判处 "+g[k]+" 负责 "+checkebox_s[k];
this.show3.append(f[k]) ;
//this.show4.append(f[k]) ;
break;
}
}
}
}catch( Exception e)
{
e.printStackTrace(); }
解决方案 »
- getLastKnownLocation got exception
- apk问题啊 急急急
- 一个TabHost能不能只有一个activity,还是分别每个tab都要有
- Excessive JNI global references,VM aborting.错误是怎么一回事?
- openGL es 网格纹理真机不显示
- 请教,拖动界面滚动条后,原videoView位置留有黑影,何故?
- 为什么我开发的安卓软件 MIUI能打开正常 安卓系统就会闪退
- 淘宝安卓客户端开发技术有哪些
- 谁见过这种类似的动画特效吗
- 华为手机锁屏后进入休眠状态,接收不到广播信息
- android保存图片问题
- android 中的各种进制 和 数据类型转换
其实你可以换个思路,算法和代码会更清晰对于s和checkebox_s分别用随机数进行打乱
然后让s[0]负责checkebox_s[0]
s[1]负责checkebox_s[1]
s[2]负责checkebox_s[2]
....
s[n]负责checkebox_s[n]如果s数组较长,则超出checkebox_s个数的部分,什么事都不干,也就是睡觉休息就是了
public static void main(String[] args) {
rand("张三,李四,王五,铁蛋,阿呆,宝贝", "吃饭,看电视,打牌,扫地");
rand("张三,李四,王五", "吃饭,看电视,打牌,扫地");
}public static void rand(String peopleList, String taskList) {
System.out.println();
String[] peoples = peopleList.split(",");
String[] tasks = taskList.split(",");
int peopleCount = peoples.length;
int taskCount = tasks.length;
if (taskCount < peopleCount) {
tasks = Arrays.copyOf(tasks, peopleCount);
taskCount = peopleCount;
System.out.println("有人可以偷懒哦");
} else {
System.out.println("每个人都有活干,谁也跑不了");
}
System.out.println("-----------------"); Random rand = new Random();
int i, j;
String task;
for (int n = 1000; n > 0; n--) {
i = rand.nextInt(taskCount);
j = rand.nextInt(taskCount);
task = tasks[i];
tasks[i] = tasks[j];
tasks[j] = task;
}
for (i = 0; i < peopleCount; i++) {
System.out.print(peoples[i] + " ");
if (tasks[i] == null) {
System.out.println("睡觉休息 ^_^");
} else {
System.out.println("负责 " + tasks[i]);
}
}
}
如果任务数比人数少,就用null补上.