简体   繁体   English

(Java) 如何根据用户输入创建一个大小的数组,并用随机数填充它

[英](Java) How to create an array with size based on user input, and also fill it with random numbers

This is the first part of aa school assignment.这是学校作业的第一部分。 I am not allowed to use ArrayList, Vector, or any other similar Java-class.我不允许使用 ArrayList、Vector 或任何其他类似的 Java 类。

I'm supposed to have a user input how many random numbers will be generated, and then the program shall create an array with that amount of random numbers in it, and print those numbers out.我应该让用户输入将生成多少个随机数,然后程序将创建一个包含该数量随机数的数组,并将这些数字打印出来。

I know how to create arrays, and how to put things into them, but I've been hitting my head against the wall of creating this specific kind of array.我知道如何创建数组,以及如何将东西放入其中,但我一直在努力创建这种特定类型的数组。 I know I can't create an array and then change the size of it once it already exists.我知道我无法创建一个数组,然后在它已经存在后更改它的大小。

Grateful for any kind of hints or flat out explanations on how to accomplish this, as I've been sitting with this for 3 days now with no idea of how to proceed.感谢任何关于如何实现这一点的提示或明确的解释,因为我已经坐了 3 天了,不知道如何继续。

I didn't want to give you EVERYTHING, but I gave you alot.我不想给你一切,但我给了你很多。 I hope this helps you understand, all you need now is to look at how to generate random numbers.我希望这能帮助你理解,你现在需要的只是看看如何生成随机数。

//This segment of code will take a number as input
Scanner input = new Scanner(System.in);
int num = input.nextInt();

//this will initialize the array
int[] arr = new int[num];

//this will let you initialize every element in array
for(int i = 0; i < arr.length; i++){

 {randomized number code}

}
Scanner scan = new Scanner(System.in);

System.out.print("Enter array length: ");
int[] arr = new int[in.nextInt()];

Random random = new Random();

for(int i = 0; i < arr.length; i++)
    arr[i] = random.nextInt(100);   // random from 0 to 99 inclusive

暂无
暂无

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

相关问题 如何用特定的随机数填充数组(java) - How to fill an array with specific random numbers (java) Java:如何根据用户输入的数组长度使用随机类创建char数组? - Java: How to create char arrays using Random Class Based on User Input on Length of Array? 如何用随机唯一数填充大小(等于用户输入)的数组? 色彩 - How to populate an array from size (equal to user input) with random unique numbers? CLOJURE 根据用户输入年份,根据用户输入为2d数组生成随机数 - Based on user input years, generate random numbers for a 2d array based on the user input 用随机数android / java填充数组 - Fill an array with random numbers android/ java 我如何创建一个带有数组的扫描仪,并在 java 中找到输入用户的最小值、最大值和平均值? - How can I create a scanner with array and also find min ,max and average of input user in java? 使用复杂度为O(n)的唯一随机数填充大小为n的Java数组 - fill an array in Java of size n with unique random numbers with complexity O(n) Java-如何生成具有随机整数的2D数组,其中网格大小为N * N(用户输入)? - Java - How to generate a 2D Array with random ints, where the grid size is N*N (User Input)? 如何获取用户输入的大小并从方法填充数组 - How to get user input on size and fill the array from a method 如何使用预定义值编辑数组以包含用户输入的JAVA - How to edit an array with predefined values to also include user input JAVA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM