我正在使用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)。
我认为这与将字符串转换为布尔值有关。我试图删除“!”但这会产生相反的效果(只说问题是错误的)。
我希望有人可以提供帮助!
的Gijs
答案 0 :(得分:2)
问题是你在Readline之前做了两次'Read',你用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();
}
答案 1 :(得分:1)
您使用了正确条件的否定,因此可以写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"
表示,因为支持运算符重载。答案 2 :(得分:1)
其他选项是使用startswith以及trim和tolower来确保用户的输入(如“a”或“A”)也得到正确处理
// try to find "a" using StartsWith
if (entered1.Trim().ToLower().StartsWith("a")) {
}
答案 3 :(得分:0)
!entered1.Equals("a")
表示其NOT "a"
。
由于你的答案总是a,b,c等测试第一个字符。这是您可以轻松测试'a'
和'A'
答案 4 :(得分: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;
}
}
答案 5 :(得分:-1)
这是你的问题:布尔值:enterbool1 =!entered1.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();
}
}