【发布时间】:2014-05-01 04:56:01
【问题描述】:
我对如何在 Java 中执行此特定过程有点困惑。
我必须使用 RNG 将特定数量的值打印到数组中,但我不知道的部分是如何为每个数组元素赋予一个值,如果 RNG 给出该值,该值将被递增。示例:
数组
0
1
2
如果RNG返回一个2,则将数组中的2递增,然后这样显示
0 1 2 1(如,它滚动了一次 2,所以它现在是 1)
我在做用户输入和 RNG 部分没有问题,但我不知道如何显示它 任何帮助将不胜感激,谢谢。
到目前为止的代码
public static 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
【问题讨论】:
-
到目前为止你的代码是什么?
-
我给你加进去了