我已将我的字符串just_numbers
转换为具有以下格式的数组格式:
System.out.println("just_numbers" + Arrays.toString(just_numbers));
看起来像[28, 27, 49, 25, 2, 23, 48, 18, 57, 58, 60, 20]
如何从上面选择3个随机数?顺序并不重要。
现在我正在尝试:
Random random = new Random();
//make random numbers
int number1 = Integer.parseInt(just_numbers[random.nextInt(just_numbers.length)]);
int number2 = Integer.parseInt(just_numbers[random.nextInt(just_numbers.length)]);
int number3 = Integer.parseInt(just_numbers[random.nextInt(just_numbers.length)]);
while (number1 != number2 && number1 != number3) {
number2 = Integer.parseInt(just_numbers[random.nextInt(just_numbers.length)]);
number3 = Integer.parseInt(just_numbers[random.nextInt(just_numbers.length)]);
}
System.out.println("random numbers are :" + number1 + " " + number2 + " " + number3);
但是我仍然得到类似的东西:
random numbers are : 25 25 58
即-重复,每个数字应只列出一次。
答案 0 :(得分:3)
private Integer[] getThreeRandomNumber() {
HashSet<Integer> integers = new HashSet<>(3);
Random random = new Random();
while (integers.size() < 3) {
// add number generation logic below
integers.add(Integer.parseInt(just_numbers[random.nextInt(just_numbers.length)]));
}
return integers.toArray(new Integer[3]);
}
答案 1 :(得分:2)
根据您编写的代码,结果是正确的。 问题在这条线上
while (number1 != number2 && number1 != number3) {
number2 = Integer.parseInt(just_numbers[random.nextInt(just_numbers.length)]);
number3 = Integer.parseInt(just_numbers[random.nextInt(just_numbers.length)]);
}
当数字不等于2或3时,您会寻找新值, 但是当number1 等于2或3时,您应该查找新值。另外,将其设为or语句(一种更好的方法如下)
bool running = true;
while(running){
running = false; // stop running if the code passes all checks
if ( a == b || a == c){
a = Integer.ParseInt(...) //new random number
running = true;
}
if ( b == c ){
b = Integer.ParseInt(...) // new random number
running = true;
}
}
答案 2 :(得分:1)
ArrayList<Integer> numbers = new ArrayList<>();
List<Integer> arrayIntegers = new ArrayList<>(Arrays.asList(28, 27, 49, 25, 2, 23, 48, 18, 57, 58, 60, 20));
Random randomGenerator = new Random();
while (numbers.size() < 3) {
int random = randomGenerator .nextInt(arrayIntegers.size());
if (!numbers.contains(arrayIntegers.get(random))) {
numbers.add(arrayIntegers.get(random));
}
}
System.out.print(" random numbers "+numbers);
答案 3 :(得分:1)
在while
循环中,条件无法完成您期望的操作。让我们从字面上解释循环:只要第一个数字与第二个数字不同,并且它也与第三个数字不同,请为number2和number3选择另一个随机数
由于条件number1!=number2
将为false
,因此将执行循环内的指令,直到第一个数字等于第二个数字为止。
请记住,数字1可以与数字2和数字3不同,但这不能防止数字2等于数字3。那里没有条件可以验证。
所以您的循环应该这样:
while (number1 == number2 || number1 == number3||number2 == number3) {
if(number1 == number2){
number2 = Integer.parseInt(just_numbers[random.nextInt(just_numbers.length)]);
}
if(number1 == number3)
{
number3 = Integer.parseInt(just_numbers[random.nextInt(just_numbers.length)]);
}
if(number2 == number3)
{
number3 = Integer.parseInt(just_numbers[random.nextInt(just_numbers.length)]);
}
}
答案 4 :(得分:1)
问题出在一段时间内,因为在以下情况下,您必须计算新的数字
所以你应该使用类似的东西
while ((number1 == number2) || (number1 == number3) || (number3 == number2))
这里的完整代码与您的问题尽可能相似:
String[] just_numbers = {"28", "27", "49", "25", "2", "23", "48", "18", "57", "58", "60", "20"};
Random random = new Random();
//make random numbers
int number1 = Integer.parseInt(just_numbers[random.nextInt(just_numbers.length)]);
int number2 = Integer.parseInt(just_numbers[random.nextInt(just_numbers.length)]);
int number3 = Integer.parseInt(just_numbers[random.nextInt(just_numbers.length)]);
while ((number1 == number2) || (number1 == number3) || (number3 == number2)) {
number2 = Integer.parseInt(just_numbers[random.nextInt(just_numbers.length)]);
number3 = Integer.parseInt(just_numbers[random.nextInt(just_numbers.length)]);
}
System.out.println("random numbers are :" + number1 + " " + number2 + " " + number3);
答案 5 :(得分:1)
int[] arr = {28, 27, 49, 25, 2, 23, 48, 18, 57, 58, 60, 20};
Random random = new Random();
List<Integer> used = new ArrayList();
used.add(random.nextInt(arr.length));
while (true && used.size() <= 3){
int temp = random.nextInt(arr.length);
if(!used.contains(temp)){
used.add(temp);
}
}
System.out.println(arr[used.get(0)] + " " + arr[used.get(1)] + " " + arr[used.get(2)] + " ");