0

我正在使用 c# 进行测试并遇到了一个“小”问题。我在网上搜索了一个小时,但发现代码不起作用:(

我正在使用 c# 进行测试并尝试在命令提示符中进行小测验

    //variables questions, Vraag is question, Solution is the solution, and Keuze is the three choices (a,b and c)

    string Vraag1 = ("Wat is de hoofstad van Oostenrijk?");
    string Solution1 = ("Wenen");
    string Keuze1 = ("a: Wenen b: Rome c: Kiev");
    string Vraag2 = ("Hoe heet de hoogste berg van Afrika?");
    string Solution2 = ("De Kilimanjaro");
    string Keuze2 = ("a: De Mount-everest b: De Kilimanjaro c: De Aconcagua");
    string Vraag3 = ("Wie was de uitvinder van de gloeilamp?");
    string Solution3 = ("Edison");
    string Keuze3 = ("a: Thomas Edison b: Albert Einstein c: Abraham Lincoln");

    //Other variables

        //entered1, 2 and 3 are variables that the user typed in
//code

    //Question 1
    Console.WriteLine("Vraag 1:");
    Console.WriteLine();
    Console.WriteLine(Vraag1);
    Console.WriteLine();
    Console.Read();
    Console.WriteLine(Keuze1);
    Console.Read();
    string entered1 = Console.ReadLine();

    Boolean enteredbool1 = !entered1.Equals("a");

    if (enteredbool1)
    {
        Console.ForegroundColor = (ConsoleColor.Green);
        Console.WriteLine("Goedzo, op naar de volgende!");
    }
    else
    {
        Console.WriteLine("FOUT!");
    }

我的问题是,如果用户回答“a”,它说它很好(goedzo),但如果他输入 b,它给出的结果相同(fout)。

我认为这与将字符串转换为布尔值有关。我试图删除“!” 但这会产生相反的效果(只说问题是错误的)。

我希望有人能帮帮忙!

吉斯

4

6 回答 6

2

问题是您在 Readline 之前执行了两次“读取”,您使用 Equals 验证的值将始终为空(“”)。这有效:

        string Vraag1 = ("Wat is de hoofstad van Oostenrijk?");
        string Solution1 = ("Wenen");
        string Keuze1 = ("a: Wenen b: Rome c: Kiev");
        string Vraag2 = ("Hoe heet de hoogste berg van Afrika?");
        string Solution2 = ("De Kilimanjaro");
        string Keuze2 = ("a: De Mount-everest b: De Kilimanjaro c: De Aconcagua");
        string Vraag3 = ("Wie was de uitvinder van de gloeilamp?");
        string Solution3 = ("Edison");
        string Keuze3 = ("a: Thomas Edison b: Albert Einstein c: Abraham Lincoln");

        //Other variables

        //entered1, 2 and 3 are variables that the user typed in
        //code

        //Question 1
        Console.WriteLine("Vraag 1:");
        Console.WriteLine();
        Console.WriteLine(Vraag1);
        Console.WriteLine();
        Console.WriteLine(Keuze1);
        string entered1 = Console.ReadLine();

        Boolean enteredbool1 = entered1.Equals("a");

        if (enteredbool1)
        {
            Console.ForegroundColor = (ConsoleColor.Green);
            Console.WriteLine("Goedzo, op naar de volgende!");
            Console.ReadLine();
        }
        else
        {
            Console.WriteLine("FOUT!");
            Console.ReadLine();
        }
于 2013-10-13T15:38:00.940 回答
1

您使用了对正确条件的否定,因此可以写Boolean enteredbool1 = entered1.Equals("a");为 ,或更短bool enteredbool1 = entered1 == "a";

为了使其更健壮,您可以例如Trim()删除字符串,删除字符串前后的空格、制表符和其他噪音。ToLower()将大写字母转换Aa,最后StartsWith检查字符串是否以正确答案开头,忽略其他噪音(例如"a: Wenen")。最终条件变为:

Boolean enteredbool1 = entered1.ToLower().Trim().StartsWith("a");

此外,一些编码建议:

  • 使用别名,Boolean等于bool
  • 字符串相等C#可以用 表示strA == "a\n",因为支持运算符重载。
  • 您提出的算法不能很好地扩展,如果您想进行测验,您需要为每个问题/答案编写代码。
于 2013-10-13T15:28:41.190 回答
1

其他选项是使用 startwith 以及 trim 和 tolower 以确保您的用户输入(如“a”或“A”)也得到正确处理

    // try to find "a" using StartsWith 
    if (entered1.Trim().ToLower().StartsWith("a")) { 
   }
于 2013-10-13T15:30:40.450 回答
0

!entered1.Equals("a")表示它的NOT "a".

由于您的答案始终是 a、​​b、c 等,因此只测试第一个字符。这是您可以轻松测试'a''A'

于 2013-10-13T15:32:28.120 回答
0

我冒昧地重写了一些东西来展示另一种方式(它是即时完成的,所以可能需要一些改进):

Dictionary<int, Dictionary<string, string>> answers = new Dictionary<int, Dictionary<string, string>>()
            {
                {1,new Dictionary<string,string>()
                       {
                           {"a","Wenen"},{"b","Rome"},
                           {"c","Kiev"},{"correct","a"}
                       }},
                 {2,new Dictionary<string,string>()
                       {
                           {"a","De Mount-everest"},{"b","De Kilimanjaro"},
                           {"c","De Aconcagua"},{"correct","b"}
                       }},
                  {3,new Dictionary<string,string>()
                       {
                           {"a","Thomas Edison"},{"b","Albert Einstein"},
                           {"c","Abraham Lincoln"},{"correct","a"}
                       }}
            };
            List<string> quiz = new List<string>()
            {
                "Wat is de hoofstad van Oostenrijk?",
                "Hoe heet de hoogste berg van Afrika?",
                "Wie was de uitvinder van de gloeilamp?"
            };           

            bool IsDone = false;
            int question = 1;
            while (!IsDone)
            {
                Console.WriteLine(question + "º: Vraag 1:");
                Console.WriteLine();
                Console.WriteLine(quiz[question - 1]);
                Console.WriteLine();
                Console.ReadKey();
                Console.WriteLine(string.Format("a: {0} b: {1} c: {2}", answers[question]["a"], answers[question]["b"], answers[question]["c"]));
                string entered1 = Console.ReadLine();

                if (entered1.ToLower() == answers[question]["correct"])
                {
                    ConsoleColor col = Console.ForegroundColor;
                    Console.ForegroundColor = (ConsoleColor.Green);
                    Console.WriteLine("Goedzo, op naar de volgende!");
                    Console.WriteLine("Continue playing?  [y] [n]");
                    if (Console.ReadLine() == "y")
                    {
                        question++;
                        Console.ForegroundColor = col;
                        continue;
                    }
                    else
                        IsDone = true;
                }
                else
                {
                    Console.WriteLine("FOUT!");
                    Console.WriteLine("Continue playing?  [y] [n]");
                    if (Console.ReadLine() == "y")
                    {
                        question++;
                        continue;
                    }
                    else
                        IsDone = true;
                }

            }
于 2013-10-13T16:25:28.107 回答
-1

这是您的问题: Boolean enteredbool1 = !entered1.Equals("a");

将其替换为: Boolean enterbool1 = enter1.Equals("a");

        string Vraag1 = ("Wat is de hoofstad van Oostenrijk?");
        string Solution1 = ("Wenen");
        string Keuze1 = ("a: Wenen b: Rome c: Kiev");
        string Vraag2 = ("Hoe heet de hoogste berg van Afrika?");
        string Solution2 = ("De Kilimanjaro");
        string Keuze2 = ("a: De Mount-everest b: De Kilimanjaro c: De Aconcagua");
        string Vraag3 = ("Wie was de uitvinder van de gloeilamp?");
        string Solution3 = ("Edison");
        string Keuze3 = ("a: Thomas Edison b: Albert Einstein c: Abraham Lincoln");

        Console.WriteLine("Vraag 1:");
        Console.WriteLine(Vraag1);
        Console.WriteLine();
        Console.WriteLine(Keuze1);
        string entered1 = Console.ReadLine();
        Boolean enteredbool1 = entered1.Equals("a");
        if (enteredbool1)
        {
            Console.ForegroundColor = (ConsoleColor.Green);
            Console.WriteLine("Goedzo, op naar de volgende!");
        }
        else
        {
            Console.WriteLine("FOUT!");
        }

        Console.Read();
    }
}
于 2013-10-13T15:44:08.843 回答