C
C#2y ago
Pesar

❔ Is there any way I can populate arrays with user input?

For example I want a user to fill up an array named randomNumbers with well, random numbers, can I do that? and if so, how?
11 Replies
Thinker
Thinker2y ago
1. Create an array 2. Loop over the array 3. For each index of the array, set the value at the index to a random number You'll need to know a couple things for this, mainly how to create arrays, how to loop over them using a for loop, and how generate random numbers using the Random class.
SinFluxx
SinFluxx2y ago
Sounds like they want the random numbers to come from the user
Pesar
PesarOP2y ago
I know how to use randoms and stuff but I want the numbers to come from the user
Thinker
Thinker2y ago
Do you know how to read user input?
Pesar
PesarOP2y ago
Like a ReadLine thing but it populates an array instead of a variable yep
Thinker
Thinker2y ago
Do you know how to turn a string into an int?
Pesar
PesarOP2y ago
Convert.toInt32/64 I believe
Thinker
Thinker2y ago
int.Parse is better, but yes So you need to create the array, loop over it, and set each element to a number inputted by the user
Pesar
PesarOP2y ago
aa I see thanks
g33b1c
g33b1c2y ago
int[] array1 = new int[5];

for(int i = 0; i < array1.Length; i++)
{
array1[i] = Convert.ToInt32(Console.ReadLine());
}
int[] array1 = new int[5];

for(int i = 0; i < array1.Length; i++)
{
array1[i] = Convert.ToInt32(Console.ReadLine());
}
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?