我想获取 随机数 但是只能是 A类:1 4 8 11 15 18里面的数在吗获取 谁知道?

解决方案 »

  1.   

    return array[new Random().nextInt(array.length)]
      

  2.   

    int i=(int)(Math.ranodm()*6)
    把1 4 8 11 15 18放入数组,用i做下标取啊,如果不要重复的可以放入一个HashSet中
      

  3.   


    int[] arr = { 1, 4, 8, 11, 15, 18 };
    int i = (int) (Math.random() * 10 % arr.length);
    System.out.println(arr[i]);
      

  4.   

    那就是3楼的办法了,把你的那些数先放到array[]中.
      

  5.   

    1楼虽然没说什么,但是让我明白了楼主的意思,呵呵。
    import java.util.Random;public class P{
    public static void main(String[] args){
    int[] a = {1, 4 ,8, 11 ,15 ,18};
    Random rand = new Random();
    int i;
    i = a[rand.nextInt(5)];}
    }
      

  6.   

    把你要的数字放到一个数组intArray=int[]里面或者List里面你随机产生一个Rodom数字=i
    你要的 int wanne = intArray[i]
    或者List(i)对i进行越界判断.
      

  7.   


    int[] a = {1, 4 ,8, 11 ,15 ,18}; 
    int s=(int) (Math.random()*a.length); 
    int i; 
    i = a[s]; 
                       System.out.println(i);