创建随机整数数组,给出 JAVA 中的长度和值范围
Create Random Int Array giving a lenght and a range of values in JAVA
我正在尝试编写一个 Java 代码,其中给定一个 int 数组的长度和一个最小值和最大值,它 returns 一个随机数组值。
我正在尝试以最简单的方式对其进行编程,但我仍然不明白编程过程是如何工作的。
我试过的是:
import java.util.Random;
public class RandomIntArray {
public static void main(String[] args){
System.out.println(createRandom(10));
}
public static int[] createRandom(int n) {
Random rd = new Random();
int[] array = new int[n];
int min = 5;
int max = 99;
for (int i = 0; i < array.length; i++) {
array[i] = rd.nextInt();
while (array[i] > min) ;
while (array[i] < max) ;
}
return array;
}
}
尝试使用IntStream
来实现你想要的
public static void main(String[] args) {
int[] arr = createRandom(10);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
public static int[] createRandom(int n) {
Random r = new Random();
int min = 5;
int max = 99;
return IntStream
.generate(() -> r.nextInt(max - min) + min)
.limit(n)
.toArray();
}
我们首先要调整的是随机数生成。您似乎指定了 min
和 max
,因此必须在这些范围内生成一个数字。我们可以使用 Random.nextInt(int bound) method to set a range equal to max - min
. However, this range will start from 0, so we must add your min
value to make the bound complete. Using this process will eliminate the need for the existing while
loops. See this question for more on this.
现在,array
中的每个值都将生成为
array[i] = rd.nextInt(max - min) + min
当尝试打印数组元素时,带有数组变量名的 System.out.println() 语句将打印数组的 内存地址 。 这不会打印整个值数组。
我们必须迭代元素以打印它们。最好使用 for 循环来执行此操作,但您的程序可以生成任意大小的数组。所以,我们应该使用 for-each 循环,如下所示:
for (int num : createRandom(10)) {
System.out.println(num);
}
这个 for-each 是说“对于 createRandom(10) 返回的 array
中的每个 int,将其作为变量 num
引用并打印它。”
这是最终代码:
import java.util.Random;
public class RandomIntArray {
public static void main(String[] args){
for (int num : createRandom(10)) {
System.out.println(num);
}
}
public static int[] createRandom(int n) {
Random rd = new Random();
int[] array = new int[n];
int min = 5;
int max = 99;
for (int i = 0; i < array.length; i++) {
array[i] = rd.nextInt(max - min) + min;
System.out.println(array[i]);
}
return array;
}
}
public static int[] createRandom(int n) {
Random rd = new Random();
int[] array = new int[n];
int min = 5;
int max = 10;
for (int i = 0; i < array.length; i++) {
array[i] = rd.nextInt(max-min+1) + min;
System.out.print(array[i] +" ");
}
return array;
}
public static void main(String[] args){
createRandom(5);
}
我正在尝试编写一个 Java 代码,其中给定一个 int 数组的长度和一个最小值和最大值,它 returns 一个随机数组值。
我正在尝试以最简单的方式对其进行编程,但我仍然不明白编程过程是如何工作的。
我试过的是:
import java.util.Random;
public class RandomIntArray {
public static void main(String[] args){
System.out.println(createRandom(10));
}
public static int[] createRandom(int n) {
Random rd = new Random();
int[] array = new int[n];
int min = 5;
int max = 99;
for (int i = 0; i < array.length; i++) {
array[i] = rd.nextInt();
while (array[i] > min) ;
while (array[i] < max) ;
}
return array;
}
}
尝试使用IntStream
来实现你想要的
public static void main(String[] args) {
int[] arr = createRandom(10);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
public static int[] createRandom(int n) {
Random r = new Random();
int min = 5;
int max = 99;
return IntStream
.generate(() -> r.nextInt(max - min) + min)
.limit(n)
.toArray();
}
我们首先要调整的是随机数生成。您似乎指定了 min
和 max
,因此必须在这些范围内生成一个数字。我们可以使用 Random.nextInt(int bound) method to set a range equal to max - min
. However, this range will start from 0, so we must add your min
value to make the bound complete. Using this process will eliminate the need for the existing while
loops. See this question for more on this.
现在,array
中的每个值都将生成为
array[i] = rd.nextInt(max - min) + min
当尝试打印数组元素时,带有数组变量名的 System.out.println() 语句将打印数组的 内存地址 。 这不会打印整个值数组。
我们必须迭代元素以打印它们。最好使用 for 循环来执行此操作,但您的程序可以生成任意大小的数组。所以,我们应该使用 for-each 循环,如下所示:
for (int num : createRandom(10)) {
System.out.println(num);
}
这个 for-each 是说“对于 createRandom(10) 返回的 array
中的每个 int,将其作为变量 num
引用并打印它。”
这是最终代码:
import java.util.Random;
public class RandomIntArray {
public static void main(String[] args){
for (int num : createRandom(10)) {
System.out.println(num);
}
}
public static int[] createRandom(int n) {
Random rd = new Random();
int[] array = new int[n];
int min = 5;
int max = 99;
for (int i = 0; i < array.length; i++) {
array[i] = rd.nextInt(max - min) + min;
System.out.println(array[i]);
}
return array;
}
}
public static int[] createRandom(int n) {
Random rd = new Random();
int[] array = new int[n];
int min = 5;
int max = 10;
for (int i = 0; i < array.length; i++) {
array[i] = rd.nextInt(max-min+1) + min;
System.out.print(array[i] +" ");
}
return array;
}
public static void main(String[] args){
createRandom(5);
}