【问题标题】:String to bool (a is true rest is false)String to bool(a 为真,其余为假)
【发布时间】:2013-10-13 15:23:11
【问题描述】:

我正在使用 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”,那么答案是正确的,您应该删除!在平等的面前。请记住,大写的“A”与 Equals 的小写“a”不同。

标签: c# string boolean


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      您使用了对正确条件的否定,因此可以写成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" 表示,因为支持运算符重载。
      • 您提出的算法不能很好地扩展,如果您想进行测验,您需要为每个问题/答案编写代码。

      【讨论】:

      • Console.ReadLine 不会在返回的字符串中附加换行符
      • 你是对的,但无论如何,我认为StartsWith 更强大,我已经更新了我的答案。
      • 问题是:我输入的任何字母都会得到相同的答案。目前尚不清楚,因为如果您键入“a”或“b”,该代码(虽然错误)无法给出相同的答案。也许问题在于大写/小写或英语的掌握。
      • 我认为主题发件人只在"b"上测试过,发现它不起作用。我认为答案中的代码是正确的,我已经用 CSharpRepl 对其进行了测试,并且它有效。
      【解决方案4】:

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

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

      【讨论】:

      • Crud,你对 Steve,“一行定义为一系列字符,后跟回车符(十六进制 0x000d)、换行符(十六进制 0x000a)或 Environment.NewLine 的值属性。返回的字符串不包含终止字符。"
      • -删除 '\n' 引用
      【解决方案5】:

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

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

      【讨论】:

        【解决方案6】:

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

        【讨论】:

        • 再检查一下,不是这个问题。它将为任何输入返回相同的值。
        猜你喜欢
        • 1970-01-01
        • 2020-05-04
        • 1970-01-01
        • 2015-03-29
        • 1970-01-01
        • 2019-10-15
        • 1970-01-01
        • 2018-07-01
        • 2018-02-03
        相关资源
        最近更新 更多