如何不再打印相同的选项?
How do I not print the same option again?
我刚刚学习了 C# 的基础知识,现在我正在尝试创建一个控制台 MCQ,它以随机顺序打印问题,并在用户每次再次参加 MCQ 时打印不同的选项。但是有时相同的选项有时会打印在一起...这是我的代码...
class Program
{
static void Wait(int sec)
{
Task.Delay(TimeSpan.FromSeconds(sec)).Wait();
}
static void printQn()
{
Questions mcq = new Questions();
Random gen = new Random();
//prints question 1
int optionCount = 1;
int x = gen.Next(5);
Console.WriteLine(mcq.questions[x]);
Wait(2);
mcq.questionsBool[x] = true;
//prints options
while (optionCount < 5)
{
int y = gen.Next(4);
if (mcq.optionsBool[x, y] == true)
{
int z = gen.Next(4);
Console.WriteLine("[" + optionCount + "]" + mcq.options[x, z]);
mcq.optionsBool[x, z] = true;
Wait(1);
optionCount++;
}
else if (mcq.optionsBool[x,y] == false)
{
Console.WriteLine("[" + optionCount + "]" + mcq.options[x, y]);
mcq.optionsBool[x, y] = true;
Wait(1);
optionCount++;
}
}
}
static void Main(string[] args)
{
printQn();
Console.ReadKey();
}
class Questions
{
public string[] questions =
{"Who was the first Queen of England?",
"What is the biggest island on Earth?",
"How many Grand Slam singles titles has Roger Federer won? ",
"When was the Euro introduced as legal currency on the world market? ",
"What year was the first Harry Potter movie released?"
};
public bool[] questionsBool = {false,false,false,false,false};
public string[,] options =
{ { "Queen Elizabeth I","Queen Mary I","Queen Anne" ,"Queen Matilda", },
{ "Hawaii" ,"Singapore" ,"Greenland" ,"Luzon ", },
{ "19" ,"17" ,"14" ,"15" , },
{ "Jan 1 1999" ,"Feb 1 1999" ,"Feb 13 1999","Feb 7 1998" , },
{ "2002" ,"1999" ,"2001" ,"2003" }};
public bool[,] optionsBool = { {false, false, false, false },
{false, false, false, false },
{false, false, false, false },
{false, false, false, false },
{false, false, false, false } };
}
}
}
我建议您使用此方法以随机顺序生成整数列表(我已经在您的代码摘录中实现了它)
//create an enumerable containing the numbers 0,1,2,3 and randomize it
Random r = new Random();
int[] options = Enumerable.Range(0, 4).OrderBy(o => r.Next()).ToArray();
//prints options
foreach (int option in options)
{
if (mcq.optionsBool[x, option] == true) //replaced 'y' with 'option'
{
int z = gen.Next(4);
Console.WriteLine("[" + optionCount + "]" + mcq.options[x, z]);
mcq.optionsBool[x, z] = true;
}
else //there are only two possible outcomes(TRUE/FALSE) so use Else instead of ElseIf
{
Console.WriteLine("[" + optionCount + "]" + mcq.options[x, option]);
mcq.optionsBool[x, option] = true;
}
Wait(1); //These two lines were present on
optionCount++; //both if and else, so I placed them outside
}
您的代码有很多可以改进/简化的地方,我建议您在 CodeReview 中展示您的代码,他们会告诉您如何改进您的代码
编辑:我无法改进您的代码,所以我会这样做:
static void printQn()
{
Questions mcq = new Questions();
Random r = new Random();
//Randomize question indexes and grab first
int[] questions = Enumerable.Range(0, mcq.questions.Length).OrderBy(q => r.Next()).ToArray();
int question = questions.First();
//Print first question
Console.WriteLine(mcq.questions[question]);
Wait(2);
//Randomize option indexes (GetLength(1) will return the size of the 'y' dimension of the options array, so 4)
int[] options = Enumerable.Range(0, mcq.options.GetLength(1)).OrderBy(o => r.Next()).ToArray();
//prints options
int optionCount = 1;
foreach (int option in options)
{
Console.WriteLine("[{0}] {1}", optionCount, mcq.options[question, option]);
Wait(1);
optionCount++;
}
}
您仍然需要实现管理用户编写的响应的部分,并将其与 optionsBool 数组进行对比以匹配正确答案,但我会把它留给您 ;)
我刚刚学习了 C# 的基础知识,现在我正在尝试创建一个控制台 MCQ,它以随机顺序打印问题,并在用户每次再次参加 MCQ 时打印不同的选项。但是有时相同的选项有时会打印在一起...这是我的代码...
class Program
{
static void Wait(int sec)
{
Task.Delay(TimeSpan.FromSeconds(sec)).Wait();
}
static void printQn()
{
Questions mcq = new Questions();
Random gen = new Random();
//prints question 1
int optionCount = 1;
int x = gen.Next(5);
Console.WriteLine(mcq.questions[x]);
Wait(2);
mcq.questionsBool[x] = true;
//prints options
while (optionCount < 5)
{
int y = gen.Next(4);
if (mcq.optionsBool[x, y] == true)
{
int z = gen.Next(4);
Console.WriteLine("[" + optionCount + "]" + mcq.options[x, z]);
mcq.optionsBool[x, z] = true;
Wait(1);
optionCount++;
}
else if (mcq.optionsBool[x,y] == false)
{
Console.WriteLine("[" + optionCount + "]" + mcq.options[x, y]);
mcq.optionsBool[x, y] = true;
Wait(1);
optionCount++;
}
}
}
static void Main(string[] args)
{
printQn();
Console.ReadKey();
}
class Questions
{
public string[] questions =
{"Who was the first Queen of England?",
"What is the biggest island on Earth?",
"How many Grand Slam singles titles has Roger Federer won? ",
"When was the Euro introduced as legal currency on the world market? ",
"What year was the first Harry Potter movie released?"
};
public bool[] questionsBool = {false,false,false,false,false};
public string[,] options =
{ { "Queen Elizabeth I","Queen Mary I","Queen Anne" ,"Queen Matilda", },
{ "Hawaii" ,"Singapore" ,"Greenland" ,"Luzon ", },
{ "19" ,"17" ,"14" ,"15" , },
{ "Jan 1 1999" ,"Feb 1 1999" ,"Feb 13 1999","Feb 7 1998" , },
{ "2002" ,"1999" ,"2001" ,"2003" }};
public bool[,] optionsBool = { {false, false, false, false },
{false, false, false, false },
{false, false, false, false },
{false, false, false, false },
{false, false, false, false } };
}
}
}
我建议您使用此方法以随机顺序生成整数列表(我已经在您的代码摘录中实现了它)
//create an enumerable containing the numbers 0,1,2,3 and randomize it
Random r = new Random();
int[] options = Enumerable.Range(0, 4).OrderBy(o => r.Next()).ToArray();
//prints options
foreach (int option in options)
{
if (mcq.optionsBool[x, option] == true) //replaced 'y' with 'option'
{
int z = gen.Next(4);
Console.WriteLine("[" + optionCount + "]" + mcq.options[x, z]);
mcq.optionsBool[x, z] = true;
}
else //there are only two possible outcomes(TRUE/FALSE) so use Else instead of ElseIf
{
Console.WriteLine("[" + optionCount + "]" + mcq.options[x, option]);
mcq.optionsBool[x, option] = true;
}
Wait(1); //These two lines were present on
optionCount++; //both if and else, so I placed them outside
}
您的代码有很多可以改进/简化的地方,我建议您在 CodeReview 中展示您的代码,他们会告诉您如何改进您的代码
编辑:我无法改进您的代码,所以我会这样做:
static void printQn()
{
Questions mcq = new Questions();
Random r = new Random();
//Randomize question indexes and grab first
int[] questions = Enumerable.Range(0, mcq.questions.Length).OrderBy(q => r.Next()).ToArray();
int question = questions.First();
//Print first question
Console.WriteLine(mcq.questions[question]);
Wait(2);
//Randomize option indexes (GetLength(1) will return the size of the 'y' dimension of the options array, so 4)
int[] options = Enumerable.Range(0, mcq.options.GetLength(1)).OrderBy(o => r.Next()).ToArray();
//prints options
int optionCount = 1;
foreach (int option in options)
{
Console.WriteLine("[{0}] {1}", optionCount, mcq.options[question, option]);
Wait(1);
optionCount++;
}
}
您仍然需要实现管理用户编写的响应的部分,并将其与 optionsBool 数组进行对比以匹配正确答案,但我会把它留给您 ;)