字符串到布尔值(a 是真的休息是假的)

本文关键字:真的 布尔值 字符串 | 更新日期: 2023-09-27 18:35:21

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

我正在使用 c# 进行测试,并尝试在命令 promt 中进行一些测验

    //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)。

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

希望有人能帮忙!

吉斯

字符串到布尔值(a 是真的休息是假的)

问题是您在 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();
        }

另一种选择是使用 startwith alongwith trim 和 tolower 来确保用户的输入(如"a"或"A")也能得到正确处理

    // try to find "a" using StartsWith 
    if (entered1.Trim().ToLower().StartsWith("a")) { 
   }
您使用了

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

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

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

此外,一些编码建议:

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

!entered1.Equals("a")表示其NOT "a"

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

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

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

这是你的问题:布尔值输入布尔值1 = !输入1。等于("a");

将其替换为:布尔值输入布尔值1 = 输入1。等于("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();
    }
}