在1到100之间的随机偶数数组

huangapple go评论123阅读模式
英文:

Random even numbers on array from 1 to 100

问题

我需要在一个数组上写入20个从1到100的随机偶数。由于某种原因,有些数组值上会得到0。对此有解决方案吗?

```java
for (int i = 0; i < 20; i++) {
    Random random = new Random();
    int randomNumber = random.nextInt(100) + 1;

    if (randomNumber % 2 == 0) {
        arregloAleatorio[i] = randomNumber; 
    }
}

打印结果,例如:

0 26 0 4 0 14 0 78 0 16 0 0 86 10 0 0 0 72 34 70

但我不希望数组中出现0。


<details>
<summary>英文:</summary>

I need to write on an array 20 random numbers from 1 to 100, but just even numbers.. For some reason i&#39;m getting 0 on some array value, any solution for this?

for (int i = 0; i < 20; i++) {

Random random = new Random();
int randomNumber = random.nextInt(100) + 1;

if (randomNumber % 2 == 0) {
arregloAleatorio[i] = randomNumber;
}



The print is, for example: 

0 26 0 4 0 14 0 78 0 16 0 0 86 10 0 0 0 72 34 70


But I don&#39;t want 0 in the array.

</details>


# 答案1
**得分**: 1

我建议先考虑约束条件。我们希望生成的数字是偶数,并且介于2和100之间。这组数字的大小为50。因此,我们可以生成一个介于0和49(包括在内)之间的随机数,然后加1并乘以2,从而得到所需的数字。代码:

```java
int[] arr = new int[20];
for (int i = 0; i < 20; i++) {
    Random random = new Random();
    int randomNumber = random.nextInt(50) + 1;
    arr[i] = randomNumber * 2;
}
英文:

I would suggest to think about the constraints first. We want to the generated numbers to be the even numbers, between 2 and 100. This group of numbers, is of the size of 50. Therefore, we can generate a random number between 0-49 (Inclusive), and then add 1 and multiply by 2, and get the required number. The code:

int[] arr = new int[20];
for (int i = 0; i &lt; 20; i++) {
    Random random = new Random();
    int randomNumber = random.nextInt(50) + 1
    arr[i] = randomNumber * 2;
}

答案2

得分: 0

尝试以下内容:

int[] arregloAleatorio = new int[20];
for (int i = 0; i < 20; i++) {
    Random random = new Random();
    int randomNumber = random.nextInt(101);
    while (randomNumber % 2 != 0 || randomNumber == 0){
        randomNumber = random.nextInt(101);
    }
    arregloAleatorio[i] = randomNumber;
}

如果数字是奇数,则不将其添加到数组中,因此默认为零。

此解决方案生成随机数,直到它不为0或奇数。

英文:

Try the following:

int[] arregloAleatorio = new int[20];
for (int i = 0; i &lt; 20; i++) {
    Random random = new Random();
    int randomNumber = random.nextInt(101);
    while (randomNumber % 2 != 0 || randomNumber == 0){
        randomNumber = random.nextInt(101);
    }
    arregloAleatorio[i] = randomNumber;
}

You are not adding a number to the array if it is odd, therefore by default it was zero.

This solution generates a random number until it is not 0 or odd.

答案3

得分: 0

因为你的迭代在数字为奇数时仍然继续进行,所以会出现这种情况;当你输出数组时,由于该索引处没有数字,它看起来像是 0!快速的解决方法就是,如果数字是奇数,只需减少 i 的值。我只添加了一行代码。我没有尝试过,但应该能够解决问题。

for (int i = 0; i < 20; i++) {

    Random random = new Random();
    int randomNumber = random.nextInt(100) + 1;

    if (randomNumber % 2 == 0) {

        arregloAleatorio[i] = randomNumber;

    } else i--;
}
英文:

This is happening because your iteration continues even if the number is odd, and when you are outputting the array, it appears to be a 0 because there is no number at that index! The quick fix would be to just decrement i if the number is odd. I am adding only 1 line of code. I didn't try it but it should work.

for (int i = 0; i &lt; 20; i++) {

    Random random = new Random();
    int randomNumber = random.nextInt(100) + 1;

    if (randomNumber % 2 == 0) {
        
        arregloAleatorio[i] = randomNumber;
        
    }else i--;

huangapple
  • 本文由 发表于 2020年9月24日 03:00:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64034651.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定