C# 每次使用时的唯一编号,但不使用。你必须在某种收集中自己追踪它们。 static void Main(string[] args) { List<string> questions = new List<st
每次使用时的唯一编号,但不使用。你必须在某种收集中自己追踪它们。C# 每次使用时的唯一编号,但不使用。你必须在某种收集中自己追踪它们。 static void Main(string[] args) { List<string> questions = new List<st,c#,asp.net,logic,combinations,C#,Asp.net,Logic,Combinations,每次使用时的唯一编号,但不使用。你必须在某种收集中自己追踪它们。 static void Main(string[] args) { List<string> questions = new List<string>(); for (int i = 0; i < 10; i++) questions.Add("Question " + i); Random r = new Ra
static void Main(string[] args)
{
List<string> questions = new List<string>();
for (int i = 0; i < 10; i++)
questions.Add("Question " + i);
Random r = new Random();
for (int i = 0; i < 3; i++)
{
int nextQuestion = r.Next(0, questions.Count);
Console.WriteLine(questions[nextQuestion]);
questions.RemoveAt(nextQuestion);
}
}
class Questions
{
const int NUMBER_OF_QUESTIONS = 10;
readonly List<string> questionsList;
private bool[] avoidQuestions; // this is the "do-not-ask-question" list
public Questions()
{
avoidQuestions = new bool[NUMBER_OF_QUESTIONS];
questionsList = new List<string>
{
"question1",
"question2",
"question3",
"question4",
"question5",
"question6",
"question7",
"question8",
"question9"
};
}
public string GetQuestion()
{
Random rnd = new Random();
int randomVal;
// get a new question if this question is on the "do not ask question" list
do
{
randomVal = rnd.Next(0, NUMBER_OF_QUESTIONS -1);
} while (avoidQuestions[randomVal]);
// do not allow this question to be selected again
avoidQuestions[randomVal] = true;
// do not allow question before this one to be selected
if (randomVal != 0)
{
avoidQuestions[randomVal - 1] = true;
}
// do not allow question after this one to be selected
if (randomVal != NUMBER_OF_QUESTIONS - 1)
{
avoidQuestions[randomVal + 1] = true;
}
return questionsList[randomVal];
}
}