V
V
Vadim Levkovich2021-11-12 16:57:58
C++ / C#
Vadim Levkovich, 2021-11-12 16:57:58

How to enter a user value into an array?

Hello! I would like to make the user use Console.ReadLine() to enter the value into the array .
Please explain in the if condition .

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-11-12
@jockermr07

I need to have the user in the console using Console.ReadLine(); he entered the value into the array cell.

Hey what's the problem.
var arraySize = int.Parse(Console.ReadLine());
var array = new int[arraySize];
for(var i = 0; i <= array.Length; i++) {
  array[i] = int.Parse(Console.ReadLine());
}

You can also use list, as edward_freedom advises , in which case you don’t have to determine the size in advance
var list = new List<int>();
// Заполняем список числами, которые вводит пользователь, пока пользователь не введёт q
var input = Console.ReadLine();
while(input != "q") {
  list.Add(int.Parse(input));
  input = Console.ReadLine();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question