2016-09-16 41 views
-3

請幫我理解, 我目前正在參加30天的代碼。我在第8天是關於C#字典的。我去運行我的代碼,但hackerrank顯示一個致命的錯誤。但是,當我在Visual Studio中運行我的代碼時,它一切正常。這是我的代碼。謝謝Hackerrank/30天的代碼/天8

using System; 
using System.Collections.Generic; 
using System.IO; 
class Solution { 
    static void Main(String[] args) { 
     /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */ 
     int N, phoneNum; 
     string name; 


     N = Convert.ToInt32(Console.ReadLine()); 

     Dictionary<string, int> phoneBook = new Dictionary<string, int>(); 
     for(int index = 0; index < N; ++index) 
     { 
      name = Console.ReadLine(); 
      phoneNum = Convert.ToInt32(Console.ReadLine()); 
      phoneBook.Add(name, phoneNum); 
     } 

     for(int index = 0; index < N; ++index) 
     { 
      name = Console.ReadLine(); 
      if(phoneBook.ContainsKey(name) == true) 
       Console.WriteLine("{0}={1}",name, phoneBook[name]); 
      else 
       Console.WriteLine("Not found"); 
     } 


    } 
} 

錯誤:

Unhandled Exception: System.FormatException: Input string was not in a correct format. at System.Number.StringToNumber (System.String str, NumberStyles options, System.NumberBuffer& number 
+2

您確定它期待C#代碼。您的代碼中有評論說:「從STDIN ... STDOUT讀取輸入」。聽起來像C++。 – Jay

+0

所以我們應該猜測「致命錯誤」是什麼? – mason

+0

你得到了什麼錯誤 –

回答

1

感謝您的回覆。在我的無知中,我沒有意識到「鑰匙」和價值是作爲一個字符串輸入的。然而,從邏輯上講,這是愚蠢的。我將有兩個特定於鍵和值的變量。這是我更新的代碼。

using System; 
using System.Collections.Generic; 
using System.IO; 
class Solution { 
    static void Main(String[] args) { 
     /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */ 
     int N; 
     string dumbAssInput, name, phoneNum; 


     N = Convert.ToInt32(Console.ReadLine()); 

     Dictionary<string, string> phoneBook = new Dictionary<string, string>(); 
     for(int index = 0; index < N; ++index) 
     { 
      dumbAssInput = Console.ReadLine(); 
      string[] keyAndValue = dumbAssInput.Split(' '); 
      name = keyAndValue[0]; 
      phoneNum = keyAndValue[1]; 
      phoneBook.Add(name, phoneNum); 
     } 

     for(int index = 0; index < N; ++index) 
     { 
      name = Console.ReadLine(); 
      if(phoneBook.ContainsKey(name) == true) 
       Console.WriteLine("{0}={1}",name, phoneBook[name]); 
      else 
       Console.WriteLine("Not found"); 
     } 


    } 
} 
0

下面這條線是一個問題:

phoneBook.Add(name, phoneNum); 

發生的事情是,如果反覆通過ReadLine數據,那麼它會嘗試添加相同Dictionary這是一個exception or Fatal error的關鍵,修改代碼爲:

if(!phoneBook.ContainsKey(name)) 
    phoneBook.Add(name, phoneNum); 

只需將其檢查Dictionary,存在Key, value pair,從而有助於避免exception

編輯

除了上面提到的問題,更改下面的代碼:

N = Convert.ToInt32(Console.ReadLine()); 

bool canConvert = int.TryParse(Console.ReadLine(),out N); 

如果canConvert是真實的,那麼N就會有一個有效的價值,或者在代碼中做出適當的決定。

phoneNum = Convert.ToInt32(Console.ReadLine()); 

canConvert = int.TryParse(Console.ReadLine(),out phoneNum); 

再次,如果canConvert是真的,那麼PHONENUM將有一個有效值,或採取在代碼中適當的決定。

+0

您在找出錯誤之前試圖回答,因此您的答案實際上並未解決問題。 – mason

+0

@mason,是什麼讓你猜如此,我在Visual Studio提供的輸入中運行程序,這導致了錯誤。像HackerRank這樣的系統總是檢查這樣簡單的測試用例。不要只是對毫無根據的假設進行投票,你能否澄清你指的確切問題。我曾經提到過的點,在代碼中是一個肯定的失敗點。 –

+0

我倒下了,因爲你沒有等待找出基本的相關細節。這個問題提到了一個「致命錯誤」,但沒有說明它是什麼。在回答之前,你應該等待這些細節。該錯誤是一個FormatException,不會將重複鍵添加到字典中。我並不是說你提到的問題不是問題,但最好等待才能在回答之前找出當前的問題。 – mason

1
int n = int.Parse(Console.ReadLine()); 
Dictionary<string,string> phoneBook = new Dictionary<string,string>(); 

for (int i = 0; i < n; i++) 
{ 
    var names = Console.ReadLine().Split(' '); 
    phoneBook.Add(names[0], names[1]); 
} 

string name = Console.ReadLine(); 
do 
{ 
    string printValue = "Not found"; 
    if (phoneBook.ContainsKey(name)) 
    { 
     printValue = name + "=" + phoneBook[name]; 
    } 

    Console.WriteLine(printValue); 

    name = Console.ReadLine(); 

}while(!string.IsNullOrEmpty(name)); 

既然有不明 .i.e。然後您將收到未知數​​量的姓名以查詢您的電話簿。