如何在Java中将int放回数组中

如何在Java中将int放回数组中,java,arrays,random,Java,Arrays,Random,基本上,我试图让rn1=0将从数组生成的随机值设置为0,但当我打印数组时,数字似乎仍然保持原样。有什么想法吗?感谢rn1=0仅在本地更改rn1的值,而不是在实际数组中。您必须将从中获得rn1的索引处的元素分配给0 // This code lets the computer select the first random number from your array int rn1 = boarduser[new Random().nextInt(boarduser.length)]

基本上,我试图让rn1=0将从数组生成的随机值设置为0,但当我打印数组时,数字似乎仍然保持原样。有什么想法吗?感谢

rn1=0仅在本地更改rn1的值,而不是在实际数组中。您必须将从中获得rn1的索引处的元素分配给0

    // This code lets the computer select the first random number from your array
  int rn1 = boarduser[new Random().nextInt(boarduser.length)];
    System.out.println("Computer's Value: " + rn1);

    // This code lets the computer select the second random number from your array      
  int rn2 = boarduser[new Random().nextInt(boarduser.length)];
    while (rn2 == rn1) // this code checks for another value in the array if the number generated was previously selected
    {
        rn2 = boarduser[new Random().nextInt(boarduser.length)];
    }
    System.out.println("Computer's Value: " + rn2);
    // This code lets the computer select the third random number from your array       
  int rn3 = boarduser[new Random().nextInt(boarduser.length)];
    while (rn3 == rn1 || rn3 == rn2)
    {
        rn3 = boarduser[new Random().nextInt(boarduser.length)];
    }
    System.out.println("Computer's Value: " + rn3);
    // turning the tokens chosen face down on the grid (changing their values to 0)
    {
        rn1 = 0;
        rn2 = 0;
        rn3 = 0;
    }

将随机索引保存到变量中,并在读取后使用它将数组中的该项归零:

int rn1Index = new Random().nextInt(boarduser.length);
int rn1 = boarduser[rn1Index];
// ...
boarduser[rn1Index] = 0;
int index = new Random().nextInt(boarduser.length)
int rn1 = boarduser[index];
boarduser[index] = 0; // you need this line