大家好
   是这样,有一个数组,我要到这个数组里随机取出一个,来对里面的值做校验,如果不通过则重新随机取一个,当然要跟刚才那个不一样,然后再做校验
   不要说for循环,关键是要随机取一个。。这个应该怎么实现,感觉不太好弄啊,条件判断跟循环一起,关键是没有循环条件
    请大家指点!!

解决方案 »

  1.   

    能不能用for循环,无限次但是一旦校验通过就break,这样行不行呢
      

  2.   

    int r =(int)(System.currentTimeMillis()%5);这个函数可以么?随即产生一个数对5取余
      

  3.   

    随机查找一个长度为100的数组,找到数组中某元素的值大于等于99时退出:
    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!");
    }
    }
      

  4.   


    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;
    }
    }
    } }
    }
      

  5.   

    把下标放到链表中,如 list(初始值0 - arr.length-1)
    1:链表长度小于1则结束(list.size()<1  return)
    产生 0到list.size()之间的随机数 num
    取出下标 tag = list.get(num)
    检测 数组arr[tag]
    如果通过则结束
    不通过则 从链表中移除tag  list.remove(num)
    继续第一步只需要循环arr.length次
      

  6.   

    下面是我编写的测试代码,里面有注释,请参考:package com.zl.base;/*
     * @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));


    }}
      

  7.   

    package tes;
    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);

    }
      

  8.   

    给你一个个人认为比较简单的方法:定义整型变量 取在数组范围内的随机数Random.nextInt()赋值给此变量 然后把这个赋值后的变量当做下标取值就Ok了,如果要判断当前产生的随机数和前一次的随机数不等的话,先保存前一次的随机数值,然后在判断一下两者是否相等就完了