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);
}
}
效果如图,如有帮助给个采纳谢谢 :
代码如下
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 可能会有相同的值。
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(); } } }输出结果如下,第一行为空,表示一个都不取
问题解决方案:
要实现从一个数组中随机选择3个数,并赋值给变量abc,其中必须将1作为其中一个随机数赋给abc,然后从剩下的数中再随机选取2个数赋给另外两个变量,可以按照以下步骤进行操作:
下面是示例代码:
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个数。