简体   繁体   English

将随机值存储到数组

[英]Storing random values to an array

Revised question: I want the even elements of my array to be stored in a corresponding array. 修改后的问题:我希望将数组的偶数元素存储在相应的数组中。 My if else statements do that. 我的if else语句可以做到这一点。 Since there will always be a varying number of evens and odds each run, I want the size of the evenArray and oddArray to adjust with each iteration of my while loop. 由于每次运行总是会有变化的偶数和赔率,因此我希望while循环的每次迭代oddArray可以调整evenArrayoddArray的大小。 I get an error when compiling that says I'm not doing that part right. 编译时出现错误,提示我没有正确执行该部分。

import java.util.Arrays;
import java.util.Random; 
public class randomdemo { 
    public static int[] randommethod()
    {
        int i = 0;

        int[] myArray;
        myArray = new int[100];

        int[] evenArray;

        int[] oddArray;

        while(i<=99)
        {
            Random rand = new Random();
            int n = rand.nextInt(25) + 0;
            myArray[i] = n;

            if(myArray[i] % 2 == 0)
            {
                evenArray = new int[i];
                evenArray[i] = n;
            }
            else
            {
                oddArray = new int[i];
                oddArray[i] = n;
            }

            i++;
        }

        return myArray;
    }

    public static void main(String args[])
    {
        int[] result = randommethod();
        System.out.println(Arrays.toString(result));
        randommethod();
    }
}

Store the result, and maybe print it. 存储结果,然后打印。 Your could use a loop or Arrays.toString(int[]) . 您可以使用循环或Arrays.toString(int[]) Something like, 就像是,

int[] result = randommethod();
System.out.println(Arrays.toString(result));

When I put both lines in main() and use your posted randommethod() it appears to work. 当我将两行都放入main()并使用您发布的randommethod()它似乎起作用。

The returned array is not being used. 返回的数组未使用。

So the return from randommethod() is an int[] but the main method does not print it (or use it in any way). 因此, randommethod()的返回randommethod()int[]但main方法不会打印(或以任何方式使用它)。

Here is one way to use it: 这是使用它的一种方法:

int[] outputRandomAry = randommethod();
for (int elem : outputRandomAry) {
  System.out.print(elem + ", ");
}
System.out.println();

Also you might want to put the Random rand = new Random(); //using the random class 另外,您可能想将Random rand = new Random(); //using the random class放进去Random rand = new Random(); //using the random class Random rand = new Random(); //using the random class outside the while loop. Random rand = new Random(); //using the random class在while循环外Random rand = new Random(); //using the random class This prevents unnecessary spinning off new objects for each rand. 这样可以防止不必要地剥离每个兰特的新对象。

And you can use int n = rand.nextInt(26); 您可以使用int n = rand.nextInt(26); for 0(inclusive) to 26(exclusive) gives you the desired range. 0(含)至26(不含)可提供所需的范围。

If you just want to print out the array without storing, write this in the main . 如果您只想打印出数组而不存储它请将其 main中 100% it will work. 100%有效。

System.out.println(Arrays.toString(randommethod()));  //print array

At this line, you returned the vaule: 在这一行,您返回了自负值:

return myArray; //returns the array

But you did not store it anywhere. 但是您没有将其存储在任何地方。 So the return value is lost. 因此,返回值将丢失。 Even though you did all the work in the method. 即使您已完成方法中的所有工作。

Store your return array as follows in the main 将返回数组存储在主目录中 ,如下所示

int[] myArray = randommethod();    //store returned value (from method)

After that, you can do anything you want with the returned array. 之后,您可以对返回的数组做任何您想做的事情。

Other than the things mentioned by other user, if I were you, I will write your method this way: 除了其他用户提到的内容之外,如果我是您,我将以这种方式编写您的方法:

public static int[] randommethod()      //declaring method
{
    Random rnd = new Random();          //using the random class
    int[] myArray = new int[100];       //create and initializing array in 1 line

    for(int x=0; x<myArray.length; x++) //Normally use for-loop when you know how many times to iterate
        myArray[x] = rnd.nextInt(26);   //0-25 has 26 possibilities, so just write 26 here

    return myArray;                     //returns the array
}

It will do exactly the same thing, I am editing this from your original codes. 它将执行完全相同的操作,我正在根据您的原始代码进行编辑。

In the main.. 在主要..

public static void main (String[] args)
{
    int[] myArray = randommethod();
}

如果您想要一个介于0到25之间的随机整数,则您的代码应为:

int n = rand.nextInt(26); //I want a random int between 0 and 25 inclusive

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM