用户输入后显示字典的键?

时间:2015-02-05 18:48:26

标签: c#

标题含糊不清,但我希望我的代码和问题会有所帮助。本质上,我希望显示我的词典 questionDict 的一个键,如果给出了某个用户输入(在这种情况下,'是')那么我希望它显示下一个键。我可以理解问题所在,只是我无法解决问题。如果我听起来很业余,请提前道歉,但那是因为我。

namespace ELIZA
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, string> questionDict = //creating dictionary for questions and animals
                new Dictionary<string, string>();
            questionDict.Add("Does it have whiskers?", "cat");
            questionDict.Add("Does it purr?", "cat");
            questionDict.Add("Does it bark?", "dog");
            foreach (string test in questionDict.Keys)
            {
                Console.WriteLine("{0}", test);
                string userInput = Console.ReadLine();
                if (userInput == "yes")
                {
                    Console.WriteLine("{0}", test);
                    Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("AW NAW");
                    Console.ReadLine();
                }
            }
        }
    }
}

编辑:问题是(控制台成绩单)

  

它有胡须吗?

     

     

它有胡须吗?

     

     

它是否发出声音?

     

     

它是否发出声音?

     

     

4 个答案:

答案 0 :(得分:3)

if块中,您再次显示test。因此你的输出将是:

> Does it have whiskers?
> yes
> Does it have whiskers?

如果你想要&#34; cat&#34;作为输出,您需要索引到字典:

if (userInput == "yes")
{
   Console.WriteLine("{0}", questionDict[test]); //Go get the value!
   Console.ReadLine();
}

哪会导致:

> Does it have whiskers?
> yes
> cat

答案 1 :(得分:1)

您应该考虑切换到使用KeyValuePair而不是仅使用string

namespace ELIZA
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, string> questionDict = //creating dictionary for questions and animals
                new Dictionary<string, string>();
            List<string> removeKeys = new List<string>();
            questionDict.Add("Does it have whiskers?", "cat");
            questionDict.Add("Does it purr?", "cat");
            questionDict.Add("Does it bark?", "dog");
            foreach (KeyValuePair<string, string> kvp in questionDict)
            {
                Console.WriteLine("{0}", kvp.Key);
                string userInput = Console.ReadLine();
                if (userInput == "yes")
                {
                    Console.WriteLine("{0}", kvp.Value);
                }
                else
                {
                    removeKeys.Add(kvp.Key);
                }
            }

            foreach(string rKey in removeKeys)
            {
                questionDict.Remove(rKey);
            }
        }
    }
}

答案 2 :(得分:1)

我仍然不确定你要做什么,但是你的问题表明你在继续之前会得到重复的打印件。所以要具体回答你的问题(但我怀疑你问的更多......)

您的密码:

foreach (string test in questionDict.Keys)
{
    Console.WriteLine("{0}", test);
    string userInput = Console.ReadLine();
    if (userInput == "yes")
    {
        Console.WriteLine("{0}", test);
        Console.ReadLine();
    }
    else
    {
        Console.WriteLine("AW NAW");
        Console.ReadLine();
    }
}

如您所见,您打印密钥,要求输入,然后再次打印密钥并再次请求输入。你应该改为:

foreach (string test in questionDict.Keys)
{
    Console.WriteLine("{0}", test);
    string userInput = Console.ReadLine();
    if (userInput == "yes")
    {
        // do what you want, store the response, etc
    }
    else
    {
        Console.WriteLine("AW NAW");
    }
}

答案 3 :(得分:0)

你可以这样:

namespace ELIZA
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, string> questionDict = //creating dictionary for questions and animals
                new Dictionary<string, string>();
            questionDict.Add("Does it have whiskers?", "cat");
            questionDict.Add("Does it purr?", "cat");
            questionDict.Add("Does it bark?", "dog");
            String lastAnswer="";
            bool isFirstQuestion=true;

            foreach (string test in questionDict.Keys)
            {
                if(isFirstQuestion)
                {
                    Console.WriteLine("{0}", test);
                    Console.ReadLine();
                    isFirstQuestion=false;
                }
                else
                {
                    if (lastAnswer == "yes")
                    {
                        Console.WriteLine("{0}", test);
                        Console.ReadLine();
                    }
                    else
                    {
                        Console.WriteLine("AW NAW");
                        Console.ReadLine();
                    }
                }
                lastAnswer = Console.ReadLine();


            }
        }
    }
}