JAVA 数组随机取值

int[] arr = {1, 2, 3, 4};
int a, b, c;
我想把 数组中 随机取3个数 赋值给abc 其中 1是必须要有的随机赋给abc,然后 2 3 4 随机再赋给其他两个变量,2 3 4 可以重复
例如:
a=1 b=2 c=2
a=2 b=3 c=1
a=3 b=1 c=3

import java.util.Random;
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4};
        int[] result = new int[3];
        Random r = new Random();
        ArrayList<Integer> lst = new ArrayList<>();
        lst.add(0);
        lst.add(1);
        lst.add(2);

        int idx = r.nextInt(3);
        result[idx] = arr[0];
        lst.remove(Integer.valueOf(idx));

        int idx1 = r.nextInt(3) + 1;
        int idx2 = r.nextInt(3) + 1;
        result[lst.get(0)] = arr[idx1];
        result[lst.get(1)] = arr[idx2];

        int a, b, c;
        a = result[0];
        b = result[1];
        c = result[2];

        System.out.println("a = " + a + ", b = " + b + ", c = " + c);
    }
}

效果如图,如有帮助给个采纳谢谢 :

img

代码如下

import java.util.Random;

public class Main {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4};
        int a, b, c;
        
        Random rand = new Random();
        
        // 随机选择一个元素作为变量a的值
        int index = rand.nextInt(arr.length);
        a = arr[index];
        
        // 再从数组中随机选择两个元素赋值给变量b和c
        b = arr[rand.nextInt(arr.length)];
        c = arr[rand.nextInt(arr.length)];
        
        System.out.println("a=" + a + " b=" + b + " c=" + c);
    }
}


import java.util.Random;

public class Main {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4};
        Random random = new Random();
        
        // 随机选择一个索引作为 a,并将对应的值赋给 a
        int aIndex = random.nextInt(arr.length);
        int a = arr[aIndex];
        
        // 随机选择两个索引作为 b 和 c,并将对应的值赋给 b 和 c
        int bIndex = random.nextInt(arr.length);
        int cIndex = random.nextInt(arr.length);
        int b = arr[bIndex];
        int c = arr[cIndex];
        
        System.out.println("a=" + a + " b=" + b + " c=" + c);
    }
}

这段代码首先创建了一个 Random 对象,然后使用 nextInt() 方法生成随机的索引。通过这些索引,从数组 arr 中获取对应的值,然后分别赋给变量 a、b 和 c。请注意,这种实现方式中,b 和 c 可能会有相同的值。



  • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/245786
  • 你也可以参考下这篇文章:Java实现通过正则表达式判断一个字符串是否含有“abc”字符串
  • 除此之外, 这篇博客: JAVA实现abc字符串的排列组合中的 3.组合:abc三个字符的所有组合 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 求所有组合也就是abc各个位是否选取的问题,第一位2中可能,第二位2种。。。所以一共有2^n种。用0表示不取,1表示选取,这样可以用110这样的形式表示ab。abc一共的表示形式从0到2^3-1。然后按位与运算,如果结果为1就输出当前位,结果0不输出。
    public class Comb {
    	public static void main(String[] args) {
    		char[] chs = {'a','b','c'};
    		comb(chs);
    	}
    
    	public static void comb(char[] chs) {
    		int len = chs.length;
    		int nbits = 1 << len;
    		for (int i = 0; i < nbits; ++i) {
    			int t;
    			for (int j = 0; j < len; j++) {
    				t = 1 << j;
    				if ((t & i) != 0) { // 与运算,同为1时才会是1
    					System.out.print(chs[j]);
    				}
    			}
    			System.out.println();
    		}
    	}
    }
    输出结果如下,第一行为空,表示一个都不取



  • 以下回答由chatgpt基于相关博客总结生成:

    问题解决方案:

    要实现从一个数组中随机选择3个数,并赋值给变量abc,其中必须将1作为其中一个随机数赋给abc,然后从剩下的数中再随机选取2个数赋给另外两个变量,可以按照以下步骤进行操作:

    1. 创建一个整数数组,将要选择的数存放在数组中。例如:{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}。
    2. 创建一个长度为3的整数数组abc,用于存放选择的随机数。
    3. 使用Random类生成随机数,通过nextInt(n)方法来获取0到n-1之间的随机整数,n为数组长度。
    4. 使用while循环,生成第一个随机数。当生成的随机数不等于1时,重新生成,直到生成的随机数等于1为止。
    5. 将第一个随机数赋值给abc数组的第一个元素:abc[0] = 随机数。
    6. 使用两个变量count和index,count记录已选择的随机数个数,index记录已选择的随机数在原数组中的下标。
    7. 进入循环,每次循环生成一个随机数,判断该随机数是否已经被选择过。如果没有被选择过,则将该随机数赋值给abc数组的剩余两个元素。
    8. 每次循环结束时,count加1,index标记该随机数已被选择。当count等于3时退出循环,完成随机选择。
    9. 最后,可以在控制台输出abc数组的值,即为随机选择的3个数。

    下面是示例代码:

    import java.util.Random;
    
    public class RandomSelection {
        public static void main(String[] args) {
            int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
            int[] abc = new int[3];
    
            Random random = new Random();
    
            // 生成第一个随机数,直到生成的随机数等于1
            int firstRandomNumber;
            do {
                firstRandomNumber = random.nextInt(numbers.length);
            } while (numbers[firstRandomNumber] != 1);
    
            abc[0] = numbers[firstRandomNumber];
    
            int count = 1;
            int index;
            while (count < 3) {
                int randomNumber = random.nextInt(numbers.length);
                index = 0;
                while (index < numbers.length) {
                    if (numbers[index] == randomNumber && !contains(abc, randomNumber)) {
                        abc[count] = randomNumber;
                        count++;
                        break;
                    }
                    index++;
                }
            }
    
            System.out.println("随机选择的3个数为:");
            for (int i = 0; i < 3; i++) {
                System.out.println(abc[i]);
            }
        }
    
        // 判断数组中是否包含指定的数字
        public static boolean contains(int[] array, int num) {
            for (int i = 0; i < array.length; i++) {
                if (array[i] == num) {
                    return true;
                }
            }
            return false;
        }
    }
    

    运行以上代码,即可得到随机选择的3个数。