我有一些未知的输入行.我知道每一行都是一个整数,我需要创建一个包含所有行的数组,例如:
输入:
12
1
3
4
5
Run Code Online (Sandbox Code Playgroud)
我需要把它作为一个数组: {12,1,3,4,5}
我有下面的代码,但我不能得到所有的行,我无法调试代码,因为我需要发送它来测试它.
List<int> input = new List<int>();
string line;
while ((line = Console.ReadLine()) != null) {
input.Add(int.Parse(Console.In.ReadLine()));
}
StockItem[] stock = new StockItem[input.Count];
for (int i = 0; i < stock.Length; i++) {
stock[i] = new StockItem(input.ElementAt(i));
}
Run Code Online (Sandbox Code Playgroud)
Sae*_*iri 15
List<int> input = new List<int>();
// first read input till there are nonempty items, means they are not null and not ""
// also add read item to list do not need to read it again
string line;
while ((line = Console.ReadLine()) != null && line != "") {
input.Add(int.Parse(line));
}
// there is no need to use ElementAt in C# lists, you can simply access them by
// their index in O(1):
StockItem[] stock = new StockItem[input.Count];
for (int i = 0; i < stock.Length; i++) {
stock[i] = new StockItem(input[i]);
}
Run Code Online (Sandbox Code Playgroud)