C# 黑客银行/30天代码/第8天

C# 黑客银行/30天代码/第8天,c#,C#,请帮助我理解, 我目前正在参加30天的代码。我在第8天,这是关于C语言的字典。我去运行我的代码,但hackerrank显示了一个致命错误。然而,当我在VisualStudio中运行我的代码时,一切都按预期工作。这是我的密码。多谢各位 using System; using System.Collections.Generic; using System.IO; class Solution { static void Main(String[] args) { /* En

请帮助我理解, 我目前正在参加30天的代码。我在第8天,这是关于C语言的字典。我去运行我的代码,但hackerrank显示了一个致命错误。然而,当我在VisualStudio中运行我的代码时,一切都按预期工作。这是我的密码。多谢各位

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");
        }


    }
}

谢谢你的回复。在我的无知中,我没有意识到键和值是作为一个字符串输入的。然而,从逻辑上讲,这是愚蠢的。我将有两个特定于key和value的变量。这是我的更新代码

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");
        }


    }
}

因为有未知的东西。然后,您将获得一个未知数量的姓名以查询您的电话簿。

您确定它需要C代码吗。代码中的注释表示从STDIN…STDOUT读取输入。听起来像C++。所以我们应该猜测致命错误是什么?你在VisualStudio中取代了黑客排名的错误是什么,你可以调试这个问题。我的程序在Visual Studio中运行良好。这是所有的东西加上错误代码。
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));