Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用户输入后是否显示字典的键?_C# - Fatal编程技术网

C# 用户输入后是否显示字典的键?

C# 用户输入后是否显示字典的键?,c#,C#,标题含糊不清,但我希望我的代码和问题能有所帮助。本质上,我希望显示dictionary questionDict的一个键,如果给出了某个用户输入(在本例中为“是”),那么我希望它显示下一个键。我能理解问题在哪里,只是我不能解决它。如果我听起来像是业余爱好者,我会提前道歉,但那是因为我是业余爱好者 namespace ELIZA { class Program { static void Main(string[] args) {

标题含糊不清,但我希望我的代码和问题能有所帮助。本质上,我希望显示dictionary 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();
                }
            }
        }
    }
}
伊丽莎 { 班级计划 { 静态void Main(字符串[]参数) { Dictionary questionDict=//为问题和动物创建字典 新字典(); 添加(“它有胡须吗?”,“猫”); 添加(“它发出呼噜声吗?”,“猫”); 添加(“它会叫吗?”,“狗”); foreach(questionDict.Keys中的字符串测试) { WriteLine(“{0}”,test); 字符串userInput=Console.ReadLine(); 如果(用户输入=“是”) { WriteLine(“{0}”,test); Console.ReadLine(); } 其他的 { 控制台写入线(“AW-NAW”); Console.ReadLine(); } } } } } 编辑:问题是(控制台记录)

它有胡须吗

它有胡须吗

它发出呜呜声吗

它发出呜呜声吗


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

> Does it have whiskers?
> yes
> Does it have whiskers?
如果希望“cat”作为输出,则需要索引到字典中:

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

> Does it have whiskers?
> yes
> cat

你应该考虑切换到使用,而不是只使用<代码>字符串< /> >

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);
            }
        }
    }
}
伊丽莎 { 班级计划 { 静态void Main(字符串[]参数) { Dictionary questionDict=//为问题和动物创建字典 新字典(); List removeKeys=新列表(); 添加(“它有胡须吗?”,“猫”); 添加(“它发出呼噜声吗?”,“猫”); 添加(“它会叫吗?”,“狗”); foreach(问题DICT中的KeyValuePair kvp) { WriteLine(“{0}”,kvp.Key); 字符串userInput=Console.ReadLine(); 如果(用户输入=“是”) { Console.WriteLine(“{0}”,kvp.Value); } 其他的 { removeKeys.Add(kvp.Key); } } foreach(removeKeys中的字符串rKey) { 移除(rKey); } } } }
我仍然不确定您想做什么,但您的问题表明,在继续之前,您会得到重复的打印输出。所以,具体回答你的问题(但我怀疑你问得更多…)

您的代码:

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

你可以有这样的东西:

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


            }
        }
    }
}
伊丽莎 { 班级计划 { 静态void Main(字符串[]参数) { Dictionary questionDict=//为问题和动物创建字典 新字典(); 添加(“它有胡须吗?”,“猫”); 添加(“它发出呼噜声吗?”,“猫”); 添加(“它会叫吗?”,“狗”); 字符串lastAnswer=“”; bool isFirstQuestion=true; foreach(questionDict.Keys中的字符串测试) { 如果(第一个问题) { WriteLine(“{0}”,test); Console.ReadLine(); isFirstQuestion=false; } 其他的 { 如果(lastAnswer==“是”) { WriteLine(“{0}”,test); Console.ReadLine(); } 其他的 { 控制台写入线(“AW-NAW”); Console.ReadLine(); } } lastAnswer=Console.ReadLine(); } } } }
您最好了解问题的根源。现在,介意和我们分享一下吗?我不知道你的问题是什么,也不知道你到底在问什么……是的,我在发帖后意识到了。它在编辑中。问题是程序在继续之前会显示两次键,如果这有意义的话。你写了两次输出,所以你会看到两次输出。啊,这对我的进一步意图很有帮助。然而,我需要它做的是先问所有这些问题,这样我以后可以使用某些值来做决定。@dave,这有点复杂。您需要将用户的响应存储在
列表
列表
中,然后对其进行迭代。你需要重新措辞或者说得更具体一些,因为我不知道如何帮助你做到这一点。你甚至不需要存储他们的回答,只需要存储他们确认的答案。或者,如果说“不”会对答案产生影响,则存储一个包含键和答案的
字典。@Pluto如果没有,这种方法也会起作用better@dave我编辑了我的答案,以符合您的逻辑:如果(userInput=“yes”),然后问下一个问题,否则停止循环;区域,有没有办法让它不中断循环,而是从字典中删除密钥?例如,如果用户对“Doit bark?”回答“no”,有没有办法从字典中删除该问题及其值?@dave我会在
For