Reputation: 93
I am trying to get a values from user and store it in a array up to particular size. my code looks like
int n = int.parse(Console.ReadLine());
string[] samples = Console.ReadLine().Split(' ');
int[] scores = Array.ConvertAll(samples, Int32.Parse);
the above code works but it doesn't stops after getting n
inputs and it allow me to store values after n
inputs. how do i make it stop getting inputs after n
inputs and i want to get all inputs in single space separated line. for eg:
9
1 2 3 4 5 6 7 8 9
should i use for loop to make that possible?
Upvotes: 2
Views: 3558
Reputation: 70652
how do i make it stop getting inputs after n inputs
Meaning, what? Exactly? According to the code, you appear to require all inputs on a single line, separated by spaces. You can ignore all inputs beyond n
like this:
string[] samples = Console.ReadLine().Split(' ').Take(n).ToArray();
If that's not what you want, please improve the question so that it's more clear exactly what you've tried, what the code you have does now, and what you want it to do instead.
Note that other alternatives include:
Console.ReadLine()
) into a loop. For example:string[] samples = new string[n];
for (int i = 0; i < n; i++)
{
samples[i] = Console.ReadLine();
}
Upvotes: 3