大家好
是这样,有一个数组,我要到这个数组里随机取出一个,来对里面的值做校验,如果不通过则重新随机取一个,当然要跟刚才那个不一样,然后再做校验
不要说for循环,关键是要随机取一个。。这个应该怎么实现,感觉不太好弄啊,条件判断跟循环一起,关键是没有循环条件
请大家指点!!
是这样,有一个数组,我要到这个数组里随机取出一个,来对里面的值做校验,如果不通过则重新随机取一个,当然要跟刚才那个不一样,然后再做校验
不要说for循环,关键是要随机取一个。。这个应该怎么实现,感觉不太好弄啊,条件判断跟循环一起,关键是没有循环条件
请大家指点!!
public class Test {
public static void main(String[] args) {
int num = 100;
int count = 0;
int[] a = new int[num];
boolean[] checked = new boolean[num];
for (int i = 0; i < num; i++) {
a[i] = i;
checked[i] = false;
}
while (count < num) {
int index = (int) (Math.random() * num);
System.out.println(index + " " + checked[index]);
if (checked[index])
continue;
if (a[index] >= num - 1) {
break;
}
checked[index] = true;
count++;
}
if (count == num) {
System.out.println("Not Found!");
} else
System.out.println("Found after " + (count + 1) + " times!");
}
}
class Arr
{
public static void main(String[] args)
{
boolean isOk = true;
//创建一个数组
int[] arr = new int[]{5,7,8,12,54};
while(isOk)
{
//产生0-100之间的随机数
int random = (int)(Math.random()*100);
for(int x = 0;x<arr.length;x++)
{
if(arr[x] == random)
{
isOk = false;
System.out.println("匹配到了" + random);
break;
}
}
} }
}
1:链表长度小于1则结束(list.size()<1 return)
产生 0到list.size()之间的随机数 num
取出下标 tag = list.get(num)
检测 数组arr[tag]
如果通过则结束
不通过则 从链表中移除tag list.remove(num)
继续第一步只需要循环arr.length次
* @author:zhanglu
* @version:2013-4-2
* @content:
* 1)随机从数组中取一个元素同给定的值比较,判断是否同给定值匹配。
* 2)如果给定的值不在数组内,则仅进行100次的比较后退出,返回无匹配值。
* 3)匹配过程中一旦匹配到值,则直接退出方法,返回匹配到了
*/public class ArrayValueCheckTest { /**
* @param args
*/
//定义校验比对的数值,可根据需要定义具体的值
private static final int CHECKVALUE = 100;
//参数数组中包含有值CHECKVALUE,则返回true
public boolean check(int[] sourceArray) {
boolean isContained = false;
//这里对校验的次数做个限制,例如仅比较100次;如果100次校验都没有匹配值则返回false
for(int i = 0;i < 100;i++) {
int j = (int)(sourceArray.length * Math.random()); //随机生成数组元素下标
//System.out.println("sourceArray[" + j + "]=" + sourceArray[j]);
if(sourceArray[j] == CHECKVALUE) {
isContained = true;
return isContained; //如果发现有匹配则推出循环,直接返回isContained
}
}
return isContained;
}
public static void main(String[] args) {
//测试数据,定义两组数组,检测他们是否包含CHECKVALUE
int[] sourceArrayContained = new int[] {0,1,3,56,1000,100};
int[] sourceArrayNoContained = new int[] {3,2,66,1100,33};
ArrayValueCheckTest containedSample = new ArrayValueCheckTest();
System.out.println("The sourceArrayContained Array is contained result: " + containedSample.check(sourceArrayContained));
System.out.println("The sourceArrayNoContained Array is contained result: " + containedSample.check(sourceArrayNoContained));
}}
import java.util.ArrayList;
import java.util.Scanner;public class Random {
public static int[] Test(){//获得测试的数组
int a[] = new int[100];
for(int i = 0; i < 100; i++){
a[i] = i + 1;
}
return a;
}
public static void getRandomTest(int a){
int[] test = Test();
ArrayList<Integer> num = new ArrayList<Integer>();//用于保存使用过的数字
int i = 0;
int randomNum = 0;
while((i++) < test.length){
int j = 1;
while(j <= num.size()){
randomNum = (int)(Math.random() * (test.length - 1));
if(test[randomNum] == num.get(j - 1)){//如果重复,则再获取一次,直到取到没重复的数字为止
randomNum = (int)(Math.random() * (test.length - 1));
j = 1;
}else{
j++;
}
}
if(j == num.size() + 1){//说明这个随机的数字没有出现过
num.add(test[randomNum]);
}
if(a == test[randomNum]){
System.out.println("true");
return;
}
}
System.out.println("false");
return;
}
public static void main(String argc[]){
System.out.println("需要匹配的数字:");
Scanner in = new Scanner(System.in);
int checkInt = in.nextInt();
getRandomTest(checkInt);
}
}