gpt4 book ai didi

c# - 为什么 String.Equals 不起作用?

转载 作者:行者123 更新时间:2023-11-30 23:02:39 26 4
gpt4 key购买 nike

我和我的 friend 正在开发一个简单的知识问答控制台应用程序,以尝试学习编写 C# 代码。我们已经让随机问题生成器正常工作,我们可以接受输入。我们遇到了一个问题,无论我们输入什么,代码总是说这是错误的答案。我们希望它像正常的琐事一样说这是正确答案或错误答案。为了检查答案是否正确,我们使用 String.Equals 来检查答案是否与存储在数组中的答案相匹配。我们很好奇我们在这里做错了什么以及如何使 if 语句正常工作。

static void Main(string[] args)
{
//Random number generator
Random rnd = new Random();
int randInt = rnd.Next(0, 9);

//Questions array Question/Answer
string[,] array2D = new string[10, 2] {
{ "Mario Kart is a video game series publish by which company?", "Nintendo" },
{ "What was the first console video game that allowed the game to be saved?", "The Legend of Zelda" },
{ "The Connecticut Leather Company later became what toy company that was popular in the 1980s for its Cabbage Patch Kids and video game consoles?", "Coleco" },
{ "Nintendo is a consumer electronics and video game company founded in what country?", "Japan" },
{ "The first person shooter video game Doom was first released in what year?", "1993" },
{ "In what year did Nintendo release its first game console in North America?", "1985" },
{ "In the world of video games, what does NES stand for?", "Nintendo Entertainment System" },
{ "In what year was the Nintendo 64 officially released?", "1996" },
{ "What was the most popular video game in the year 1999?", "Doom" },
{ "If you have any kind of console known to mankind, what will your mother call it no matter what?", "A Nintendo" } };

//printing the question
System.Console.WriteLine(array2D[randInt, 0]);
System.Console.ReadKey();

//taking user input
string Answer = Console.ReadLine();

//checking to see if the answer is correct
if (Answer.Equals(array2D[randInt, 1]))
{
//the console exclamation for the right answer given in any scenario.
Console.WriteLine("Correct Answer! Good job!");
Console.ReadKey();
}
else
{
//the console exlamation for the incorrect answer in any given scnario
Console.WriteLine("Aw man! You got it wrong! Better luck next time :(");
Console.WriteLine("The correct answer was {0}", array2D[randInt, 1]);
Console.ReadKey();
}

}

最佳答案

只需删除上面的System.Console.ReadKey()

//Random number generator
Random rnd = new Random();
int randInt = rnd.Next(0, 9);

//Questions array Question/Answer
string[,] array2D = new string[10, 2] {
{ "Mario Kart is a video game series publish by which company?", "Nintendo" },
{ "What was the first console video game that allowed the game to be saved?", "The Legend of Zelda" },
{ "The Connecticut Leather Company later became what toy company that was popular in the 1980s for its Cabbage Patch Kids and video game consoles?", "Coleco" },
{ "Nintendo is a consumer electronics and video game company founded in what country?", "Japan" },
{ "The first person shooter video game Doom was first released in what year?", "1993" },
{ "In what year did Nintendo release its first game console in North America?", "1985" },
{ "In the world of video games, what does NES stand for?", "Nintendo Entertainment System" },
{ "In what year was the Nintendo 64 officially released?", "1996" },
{ "What was the most popular video game in the year 1999?", "Doom" },
{ "If you have any kind of console known to mankind, what will your mother call it no matter what?", "A Nintendo" } };

//printing the question
System.Console.WriteLine(array2D[randInt, 0]);
//System.Console.ReadKey(); Removed

//taking user input
string Answer = Console.ReadLine();

//checking to see if the answer is correct
if (Answer.Equals(array2D[randInt, 1]))
{
//the console exclamation for the right answer given in any scenario.
Console.WriteLine("Correct Answer! Good job!");
Console.ReadKey();
}
else
{
//the console exlamation for the incorrect answer in any given scnario
Console.WriteLine("Aw man! You got it wrong! Better luck next time :(");
Console.WriteLine("The correct answer was {0}", array2D[randInt, 1]);
Console.ReadKey();
}

关于c# - 为什么 String.Equals 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50360574/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com