我必须创建一个以随机顺序打印问题的测验。我已经在if语句中写出了所有问题,如果已经回答了10个问题,我希望显示所有问题,并给出正确的答案和用户输入的答案进行比较。我还不知道怎么做,所以代码在1完成后转到下一个if语句。
如果声明(问题)和随机数生成器指向另一个if if等,我如何完成1。
static void Main(string[] args)
{
string answer1;
string answer2;
string answer3;
string answer4;
string answer5;
string answer6;
string answer7;
string answer8;
string answer9;
string answer10;
int answeredQs = 0;
Random rnd = new Random();
int questionNum = rnd.Next(1,10);
Console.WriteLine("Question Number: " + questionNum);
if (questionNum == 1)
{
Console.WriteLine("What is a CPU?");
answer1 = Console.ReadLine();
answeredQs = +1;
}
if (questionNum == 2)
{
Console.WriteLine("What does 'RAM' stand for?");
answer2 = Console.ReadLine();
answeredQs = answeredQs + 1;
}
if (questionNum == 3)
{
Console.WriteLine("What is RAM?");
answer3 = Console.ReadLine();
answeredQs = answeredQs + 1;
}
if (questionNum == 4)
{
Console.WriteLine("How do you measure how fast a processor is?");
answer4 = Console.ReadLine();
answeredQs = answeredQs + 1;
}
if (questionNum == 5)
{
Console.WriteLine("What is an ALU & what does it do?");
answer5 = Console.ReadLine();
answeredQs = answeredQs + 1;
}
if (questionNum == 6)
{
Console.WriteLine("What is a register?");
answer6 = Console.ReadLine();
answeredQs = answeredQs + 1;
}
if (questionNum == 7)
{
Console.WriteLine("What is EEPROM?");
answer7 = Console.ReadLine();
answeredQs = answeredQs + 1;
}
if (questionNum == 8)
{
Console.WriteLine("What is the difference between SRAM and DRAM?");
answer8 = Console.ReadLine();
answeredQs = answeredQs + 1;
}
if (questionNum == 9)
{
Console.WriteLine("What is ROM?");
answer9 = Console.ReadLine();
answeredQs = answeredQs + 1;
}
if (questionNum == 10)
{
Console.WriteLine("What does the Control Unit do?");
answer10 = Console.ReadLine();
answeredQs = answeredQs + 1;
}
if (answeredQs == 10)
{
Console.WriteLine("asdasdasd");
}
答案 0 :(得分:2)
您可以将if
语句放在循环中(for
,while
...)
此外,例如,通过将问题放在ArrayList中(或任何其他“数据存储”结构,例如简单数组),您的代码可以更具可读性。
然后,您可以使用此结构中的问题的索引,始终处于循环中。
希望这有帮助