C
C#9mo 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
Thinker9mo 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
SinFluxx9mo ago
Sounds like they want the random numbers to come from the user
Pesar
Pesar9mo ago
I know how to use randoms and stuff but I want the numbers to come from the user
Thinker
Thinker9mo ago
Do you know how to read user input?
Pesar
Pesar9mo ago
Like a ReadLine thing but it populates an array instead of a variable yep
Thinker
Thinker9mo ago
Do you know how to turn a string into an int?
Pesar
Pesar9mo ago
Convert.toInt32/64 I believe
Thinker
Thinker9mo 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
Pesar9mo ago
aa I see thanks
g33b1c
g33b1c9mo 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
Accord9mo 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.
Want results from more Discord servers?
Add your server
More Posts