C
C#16mo 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
Thinker16mo 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
SinFluxx16mo ago
Sounds like they want the random numbers to come from the user
Pesar
PesarOP16mo ago
I know how to use randoms and stuff but I want the numbers to come from the user
Thinker
Thinker16mo ago
Do you know how to read user input?
Pesar
PesarOP16mo ago
Like a ReadLine thing but it populates an array instead of a variable yep
Thinker
Thinker16mo ago
Do you know how to turn a string into an int?
Pesar
PesarOP16mo ago
Convert.toInt32/64 I believe
Thinker
Thinker16mo 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
PesarOP16mo ago
aa I see thanks
g33b1c
g33b1c16mo 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
Accord16mo 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?