1.i必須獲取輸入並將其存儲到字典中,並再次獲取一些輸入並檢查它是否在字典中。
2.我試過這個,但給出了錯誤(輸入字符串格式不正確)。
3.它在編譯時沒有提供任何錯誤,但它在運行時給出錯誤。這是什麼問題。
4.我的輸入是: - SAM 99912222
湯姆11122222
哈里12299933
SAM
愛德華
哈里
5.代替讀的()。我也試過ReadLine(),但問題是一樣的。在我的C#代碼中獲取運行時錯誤
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{
static void Main()
{
int n = Convert.ToInt32(Console.Read());
Dictionary<string, int> phbook = new Dictionary<string, int>();
for (int i = 0; i < n; i++)
{
string name = Console.Read().ToString();
int phonno = Convert.ToInt32(Console.ReadLine());
phbook.Add(name, phonno);
}
foreach (var keypairs in phbook)
{
string namet = Console.Read().ToString();
if (phbook.ContainsKey(namet))
{
Console.Write("{0}={1}", namet, phbook[namet]);
}
else
{
Console.Write("Not found");
}
}
}
}
完整的錯誤是
Unhandled Exception:
System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number+NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) [0x00057] in <a07d6bf484a54da2861691df910339b1>:0
at System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) [0x00015] in <a07d6bf484a54da2861691df910339b1>:0
at System.Int32.Parse (System.String s, System.IFormatProvider provider) [0x00008] in <a07d6bf484a54da2861691df910339b1>:0
at System.Convert.ToInt32 (System.String value) [0x0000b] in <a07d6bf484a54da2861691df910339b1>:0
at Solution.Main() [0x00034] in solution.cs:15
[ERROR] FATAL UNHANDLED EXCEPTION: System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number+NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) [0x00057] in <a07d6bf484a54da2861691df910339b1>:0
at System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) [0x00015] in <a07d6bf484a54da2861691df910339b1>:0
at System.Int32.Parse (System.String s, System.IFormatProvider provider) [0x00008] in <a07d6bf484a54da2861691df910339b1>:0
at System.Convert.ToInt32 (System.String value) [0x0000b] in <a07d6bf484a54da2861691df910339b1>:0
at Solution.Main() [0x00034] in solution.cs:15
您的輸入是什麼? –
由Console.ReadLine()輸入的東西可能無法轉換爲int –
@HiranPerera我已經用輸入重新編輯了問題。 – shindvii