我在c#中创建了一个测验并且只有一个问题,因为我通过测验它在一轮中不止一次重复同样的问题并且不知道如何解决这个问题,因为我是新的学习c#。希望有人可以帮助我!!
我的代码如下:
public DrivingTestQuestions(string Name, string Gender, string DOB, string Address, string Postcode, string Phone, string Email)
{
InitializeComponent();
this.lblName.Text = Name;
this.Gender = Gender;
this.DOB = DOB;
this.Address = Address;
this.Postcode = Postcode;
this.Phone = Phone;
this.Email = Email;
this.correct = 0;
this.position = 0;
this.random = new Random();
Dictionary<int, Quiz> dictionary;
dictionary = new Dictionary<int, Quiz>();
dictionary.Add(0, new Quiz("A vehicle should not send out visible smoke for more than?", "12 seconds", "10 seconds", "20 seconds", "5 seconds", "10 seconds"));
dictionary.Add(1, new Quiz("Who can require you to undergo a breath screening test?", "An ambulance driver", "Other drivers", "Fire Crew", "A police officer", "A police officer"));
dictionary.Add(2, new Quiz("When passing a bus displaying a School sign that has stopped to let children on or off, what should your speed be?", "20kph", "50kph", "70kph", "100kph", "20kph"));
dictionary.Add(3, new Quiz("What is the speed limit from the time you pass an Accident sign until you have passed the crash site?", "30kph", "45kph", "25kph", "20kph", "20kph"));
dictionary.Add(4, new Quiz("If your car was first registered before 1 January 2000, how often must you renew its warrant of fitness?", "Three Months", "Six Months", "Tweleve Months", "Twenty four Months", "Six Months"));
dictionary.Add(5, new Quiz("What should you do when another vehicle is following you very closely?", "Move to the left and slow down to make it easier for it to pass", "Speed up so you're not holding it up", "Pump your brakes to tell them to drop back", "Put your hazard lights on", "Move to the left and slow down to make it easier for it to pass"));
dictionary.Add(6, new Quiz("If you are driving a car, what should you do when following another vehicle in wet conditions?", "Observe the two-second rule", "Observe the four-second rule", "Observe the six-second rule", "Try to overtake so that you are not blinded by the spray", "Observe the four-second rule"));
dictionary.Add(7, new Quiz("How many demerit points do you receive for exceeding the speed limit by 11-20kph?", "10", "20", "30", "40", "20"));
dictionary.Add(8, new Quiz("If there are one or more yellow flashing lights at traffic signals, what does it mean?", "All cars must stop as emergency vehicles are coming through", "The lights are faulty and the Give Way rules apply", "There's a diversion ahead", "Move to the left hand side of the road", "The lights are faulty and the Give Way rules apply"));
dictionary.Add(9, new Quiz("When passing a horse and rider, what should you do?", "Give a quick toot of the horn to warn them you're coming", "Pass as quickly as possible to avoid spooking the horse", "Slow down, giving them as much room as you can", "Turn on your headlights to warn the rider", "Slow down, giving them as much room as you can"));
this.questions = dictionary;
return;
}
public void GetQuestion()
{
this.position = this.random.Next(0,10);
{
this.lblQuestion.Text = this.questions[this.position].thequestion;
this.radBtnOption1.Text = this.questions[this.position].option1;
this.radBtnOption2.Text = this.questions[this.position].option2;
this.radBtnOption3.Text = this.questions[this.position].option3;
this.radBtnOption4.Text = this.questions[this.position].option4;
this.ResetAll();
return;
}
答案 0 :(得分:0)
您的代码看起来不错,使用Random()
看起来是正确的。如果你执行random.Next(0,10)
,它将在范围[0..9]中返回一个随机int。很可能同一个int返回几次,甚至可能你连续得到10个零,这就是随机性。
如果您想避免两次看同一个问题,则需要稍微不同的算法:
将问题标记为“看到”并绘制另一个随机数,如果你得到一个“看得见”的。
-Or -
保留一个“看不见的问题”列表,并从此列表中提取下一个问题,在“完成”后将其从此列表中删除。
答案 1 :(得分:0)
另一种解决方法是保留一个存储已提问题索引的列表,下面是一个简单的例子,说明如何避免已经问过的问题:
using System;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
Random rnd = new Random();
List<int> AlreadyAskedQuestions = new List<int>();
for(int i = 0 ; i<10;i++)
{
int rn = rnd.Next(0,10);
while(AlreadyAskedQuestions.Contains(rn))
{
rn = rnd.Next(0,10);
}
AlreadyAskedQuestions.Add(rn);
}
foreach(int i in AlreadyAskedQuestions)
{
Console.WriteLine(i);
}
}
}
答案 2 :(得分:0)
将所有问题存储在一个单独的列表中
然后你必须像其他人已经建议的那样做一个小的工作区:
在您存储问题的List
处添加List<int> askedQuestions = new List<int>();
并更改此
public void GetQuestion() { if(askQuestion.Count!= questions.Count) { //不好,但这应该有效 do {this.position = this.rand.Next(0,10); } while(askQuestions.Contains(this.position));
{
this.lblQuestion.Text = this.questions[this.position].thequestion;
this.radBtnOption1.Text = this.questions[this.position].option1;
this.radBtnOption2.Text = this.questions[this.position].option2;
this.radBtnOption3.Text = this.questions[this.position].option3;
this.radBtnOption4.Text = this.questions[this.position].option4;
this.ResetAll();
askedQuestions.add(this.position);//the question is marked as asked
return;
}
}
}
你的词典中有一堆问题,你总是随机选择其中一个。为了防止某些问题出现两次,您必须从此池中删除问题。
为了使问题变得更简单,dictionary
不是最佳解决方案:只需使用List
,一切都可以:)
因此,您必须将Dictionary
更改为List
,但这对您来说不是一件大事。在您的特定情况下,List
是一种更好的解决方案,因为您使用普通Dictionary
作为List
。使用Dictionary
意味着您的计算机必须在条目中搜索需要时间的内容。使用List
表示您的计算机已经知道在哪里查找条目。
正如你使用正常的indizes(这正是List
所做的那样)作为你的词典的一个键,它不会有任何区别。
public void GetQuestion()
{
if(questions.Count != 0)
{
this.position = this.random.Next(0, questions.Count);
{
this.lblQuestion.Text = this.questions[this.position].thequestion;
this.radBtnOption1.Text = this.questions[this.position].option1;
this.radBtnOption2.Text = this.questions[this.position].option2;
this.radBtnOption3.Text = this.questions[this.position].option3;
this.radBtnOption4.Text = this.questions[this.position].option4;
this.ResetAll();
qestions.RemoveAt(position);//important!
return;
}
}
}
另一件事可能是一个新的quizz的开始,因为池中不再有任何问题。我目前不知道你是如何做到的,但一个可能的解决方案是再次添加问题:
if (this.Question == 10)
{
SummaryofDrivingTest form = new SummaryofDrivingTest(this.lblName.Text, this.Gender, this.DOB, this.Address, this.Postcode, this.Phone, this.Email, Convert.ToString(this.correct));
form.Show();
this.Hide();
List<Quiz> dictionary;
dictionary = new Dictionary<int, Quiz>();
dictionary.Add(new Quiz("A vehicle should not send out visible smoke for more than?", "12 seconds", "10 seconds", "20 seconds", "5 seconds", "10 seconds"));
dictionary.Add(new Quiz("Who can require you to undergo a breath screening test?", "An ambulance driver", "Other drivers", "Fire Crew", "A police officer", "A police officer"));
dictionary.Add(new Quiz("When passing a bus displaying a School sign that has stopped to let children on or off, what should your speed be?", "20kph", "50kph", "70kph", "100kph", "20kph"));
dictionary.Add(new Quiz("What is the speed limit from the time you pass an Accident sign until you have passed the crash site?", "30kph", "45kph", "25kph", "20kph", "20kph"));
dictionary.Add(new Quiz("If your car was first registered before 1 January 2000, how often must you renew its warrant of fitness?", "Three Months", "Six Months", "Tweleve Months", "Twenty four Months", "Six Months"));
dictionary.Add(new Quiz("What should you do when another vehicle is following you very closely?", "Move to the left and slow down to make it easier for it to pass", "Speed up so you're not holding it up", "Pump your brakes to tell them to drop back", "Put your hazard lights on", "Move to the left and slow down to make it easier for it to pass"));
dictionary.Add(new Quiz("If you are driving a car, what should you do when following another vehicle in wet conditions?", "Observe the two-second rule", "Observe the four-second rule", "Observe the six-second rule", "Try to overtake so that you are not blinded by the spray", "Observe the four-second rule"));
dictionary.Add(new Quiz("How many demerit points do you receive for exceeding the speed limit by 11-20kph?", "10", "20", "30", "40", "20"));
dictionary.Add(new Quiz("If there are one or more yellow flashing lights at traffic signals, what does it mean?", "All cars must stop as emergency vehicles are coming through", "The lights are faulty and the Give Way rules apply", "There's a diversion ahead", "Move to the left hand side of the road", "The lights are faulty and the Give Way rules apply"));
dictionary.Add(new Quiz("When passing a horse and rider, what should you do?", "Give a quick toot of the horn to warn them you're coming", "Pass as quickly as possible to avoid spooking the horse", "Slow down, giving them as much room as you can", "Turn on your headlights to warn the rider", "Slow down, giving them as much room as you can"));
this.questions = dictionary;
return;
}
正如您对C#所了解的那样,有一点:
你不需要总是在方法结束时写return
:)
当你这样做时,它完全没问题,但你不需要这样做(除非你真的需要返回一个值)。