简体   繁体   English

将随机数值打印到数组

[英]Printing random number values to an Array

Im a bit confused on how to do this particular process in Java. 我对如何在Java中执行此特定过程有些困惑。

I have to use a RNG to print a specific amount of values to an array, but the part I can't figure out is how to give each array element a value that is to be incremented if the RNG gives that value. 我必须使用RNG将特定数量的值打印到数组,但是我不知道的部分是如何给每个数组元素一个值,如果RNG给出该值,该值将递增。 Example: 例:

Array 数组

0 0
1 2 1 2

If the RNG returns a 2, then increment the 2 in the array, and then display it like this 如果RNG返回2,则在数组中增加2,然后像这样显示它

0 1 2 1 (as in, it rolled a 2 once so its now 1) 0 1 2 1(例如,它滚动了2次,所以现在滚动了1次)

I have no problems doing the user input and RNG part, but I don't know how to display it like that Any help would be appreciated, thanks. 我在处理用户输入和RNG部分时没有问题,但是我不知道如何显示它,谢谢您的帮助。

Code so far 到目前为止的代码

public static void main(String[] args) { 公共静态void main(String [] args){

    Scanner input = new Scanner( System.in); //Declares the scanner
    Random randomNumbers = new Random(); // Declares the random property you will need later

    //Variables
    int randomrequest = 0; //Declares randomnum as a integer with a value of 0, this is what the user inputs as the number they want.
    int randomrange = 0; //Declares the number for the number range for the random number generator
    int randomcounter = 0;//Declares the counter you will need later as 0
    int result = 0; //This variable is for the random number generation result


    System.out.printf( "How many random numbers do you want to generate?" ); //asks for the number to generate

    randomrequest = input.nextInt(); //Makes the user enter a value to and then stores it in this variable.

    System.out.printf( "What is the number of values for each random draw?" ); //asks for the number range

    randomrange = input.nextInt(); //See above

    //Ok now we have to use the inputed information to do the rest of the processing before we can display it

    //First, create the array and give it the size equal to the number range entered

    int[] array = new int[ randomrange ]; // Declares the array with the amount of slots for each outcome

    //We need to use a loop here to make sure it rolls the RNG enough times 

    while (randomcounter != randomrequest) { //This tells it generate a random number until the counter equals the entered amount.

        result = randomNumbers.nextInt( randomrange ); //Generates a random number within the range given


        randomcounter += 1; //increments the counter, so that it will eventually equal the entered number, and stop.

    }//End of do while


    }//end of Public static void

}//End of entire class

If I'm interpreting your question correctly, one thing you can try is to have each element in the array be a counter for its index. 如果我正确地解释了您的问题,那么您可以尝试做的一件事就是让数组中的每个元素成为其索引的计数器。 So if your random number generator produces a 2, you increment the value in array[2] . 因此,如果您的随机数生成器生成一个2,则将array[2]的值递增。

A concise way to put it might be: 一种简洁的表达方式可能是:

while (randomCounter++ != randomRequest) {
    array[randomNumbers.nextInt(randomRange)]++;
}

The following code should work for your solution: 以下代码适用于您的解决方案:

while (randomcounter != randomrequest) {
    result = randomNumbers.nextInt(randomrange);
    array[result] += 1;
    randomcounter +=1;
    for (int i = 0; i < array.length; i++)
    {
        system.out.print(array[i] + " ");
    }
    system.out.println();
}

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

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