2012-04-29 12 views
5

Ho un numero sconosciuto di righe di input. So che ogni linea è un numero intero, e ho bisogno di fare un array con tutte le righe, per esempio:Leggere un numero indefinito di righe dall'ingresso standard

ingresso:

12 
1 
3 
4 
5 

e devo farlo come un array: {12,1,3,4,5}

Ho il codice seguente, ma non riesco a ottenere tutte le linee e non riesco a eseguire il debug del codice perché devo inviarlo per testarlo.

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)); 
} 
+1

Dare LINQPad una prova, è possibile compilare e pseudo-debug facilmente senza VS. http://www.linqpad.net/ –

+0

Mi dispiace non averlo visto, il mio correttore ortografico è impostato in spagnolo e tutto il mio testo è con il pennarello rosso, mi dispiace per quello. – Santanor

+1

[Ideone] (http://ideone.com) è anche utile se è necessario fornire input. – Ryan

risposta

11
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]); 
} 
+6

'while (! String.IsNullOrEmpty (riga = Console.ReadLine()))' sembrerebbe più bello :) – Ryan

+0

@minitech, Sì, all'inizio pensavo di scriverlo nel modo in cui hai scritto, ma dopo quello che ho pensato potrebbe essere un po 'di confusione. –

+1

Il problema non è la sintassi, non sto iniziando con C# ma non so perché questa soluzione non funziona (no, la tua soluzione non ha funzionato neanche) :(Continuerò a cercare Grazie, – Santanor

2

Ti veramente bisogno gli ID in un array? Io probabilmente provare qualcosa di simile:

// Use a function that takes a StringReader as an input. 
    // That way you can supply test data without using the Console class. 
    static StockItem[] ReadItems(StringReader input) 
    { 
     var stock = new List<StockItem>(); 

     // Only call ReadLine once per iteration of the loop. 
     // I expect this is why you're not getting all the data. 
     string line = input.ReadLine(); 
     while(! string.IsNullOrEmpty(line)) { 

     int id; 
     // Use int.TryParse so you can deal with bad data. 
     if(int.TryParse(line, out id)) { 
      stock.Add(new Stock(id)); 
     } 

     line = input.ReadLine(); 
     } 

     // No need to build an populate an array yourself. 
     // There's a linq function for that. 
     return stock.ToArray(); 
    } 

Poi si può chiamare con

var stock = ReadItems(Console.In); 
Problemi correlati