2012-04-29 23 views
5

से लाइनों की एक अनिर्धारित संख्या पढ़ें मेरे पास इनपुट की एक अज्ञात संख्या है। मुझे पता है कि प्रत्येक पंक्ति एक पूर्णांक है, और मैं उदाहरण के लिए, सभी लाइनों के साथ एक सरणी बनाने की जरूरत है:मानक इनपुट

इनपुट:

12 
1 
3 
4 
5 

और मैं एक सरणी के रूप में यह प्राप्त करने की आवश्यकता: {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)); 
} 
+1

साथ कॉल कर सकते हैं LINQPad आज़मा कर देखें, तो आप बनाम बिना आसानी से संकलन और छद्म डिबग कर सकते हैं http://www.linqpad.net/ –

+0

मुझे खेद है कि मैंने इसे नहीं देखा, मेरा वर्तनी परीक्षक स्पेनिश में सेट किया गया है और मेरा पूरा पाठ लाल मार्कर के साथ है, इसके बारे में खेद है। – Santanor

+1

[आइडिया] (http://ideone.com) भी अच्छा है यदि आपको इनपुट प्रदान करने की आवश्यकता है। – Ryan

उत्तर

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

'जबकि (! String.IsNullOrEmpty (line = Console.ReadLine())) 'nicer देखेंगे :) – Ryan

+0

@minitech, हां, पहले मैंने सोचा था कि जिस तरह से आपने लिखा था उसे लिखो, लेकिन उसके बाद मैंने सोचा कि यह हो सकता है थोड़ा उलझन में –

+1

समस्या सिंटैक्सिस नहीं है, मैं सी # से शुरू नहीं कर रहा हूं लेकिन मुझे नहीं पता कि यह समाधान क्यों काम नहीं करते हैं (नहीं, आपका समाधान या तो काम नहीं करता है) :(मैं तलाश जारी रखने जा रहा हूं यह, धन्यवाद !! – Santanor

2

आप वास्तव में एक सरणी में आईडी की जरूरत है? मैं शायद कुछ इस तरह की कोशिश करेंगे:

// 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(); 
    } 

तो फिर तुम यह

var stock = ReadItems(Console.In); 
संबंधित मुद्दे