gpt4 book ai didi

c++ - 总是重复随机选择

转载 作者:行者123 更新时间:2023-11-28 07:01:52 25 4
gpt4 key购买 nike

我这里有一个代码。目标是创建一个多项选择测验,其中问题来自文本文件,并将随机选择以显示。我能够做多项选择,问题是当它生成时,已经选择并显示的问题再次显示。选项也来自文本文件的答案,但问题是选项在问题中重复显示。

所以我想知道如何不再显示pick问题,以及如何不重复显示选项。

示例输出

1. what month do we celebrate Christmas?
a.december
b.december
c.january
d.december



//Randomizes the questionList vector
random_shuffle(questionList.begin(), questionList.end());
//Goes through every Test question

for(int i = 0; i < questionList.size(); i++){
vector <Test> randomAnswer;
//Puts the correct Answer into it first
randomAnswer.push_back(questionList[i]);
//Then randomly gets 3 other answers from questionList
while(randomAnswer.size() < 4)
{
int random = rand();
if(random != i){
randomAnswer.push_back(questionList[rand() % (questionList.size() - 1)]);
}
//Shuffle the answers
random_shuffle(randomAnswer.begin(), randomAnswer.end());
//Print the question
cout << questionList[i].getQuestion() << ":" << endl;
//Initialize the first choice character to 'A'
char ch = 'A';
//Prints the shuffled answers
for(int j = 0; j < randomAnswer.size(); j++)
{
cout << ch << ") " << randomAnswer[j].getAnswer() << endl;
//Increment 'A' so it can print 'B' and so forth
++ch;
}
//Get users response
cout << "\nYour answer: ";
cin >> response;
//Bool data type to determine if the correct answer was found
bool isCorrect = false;
switch(toupper(response))
{
case 'A':
if(randomAnswer[0].getAnswer()==questionList[i].getAnswer())
isCorrect = true;
break;
case 'B':
if(randomAnswer[1].getAnswer()==questionList[i].getAnswer())
isCorrect = true;
break;
case 'C':
if(randomAnswer[2].getAnswer()==questionList[i].getAnswer())
isCorrect = true;
break;
case 'D':
if(randomAnswer[3].getAnswer()==questionList[i].getAnswer())
isCorrect = true;
break;
default:
cout << "\nIncorrect input.\n";
}
//If the answer was found print "Correct" else "Wrong"
if(isCorrect)
{
cout << "\nYou got the answer correct!\n";
}
else
{
cout << "\nYou got the answer WRONG!\n"
<< "Correct answer was " << questionList[i].getAnswer() <<
endl;

最佳答案

您的方法不正确,您在列表中插入正确答案,然后随机选择三个答案。视觉示例(1 是正确的):

1 2 3 4
Insert 1 into randomAnswer
Randomly pick three numbers and insert them into randomAnswer: ex (2, 1, 3)

正确的做法是将所有四个答案都取下来,然后将它们打乱。伪代码

swap(questionList[i], *(questionList.end() - 1));
Insert questionList[i]
Shuffle questionList.begin() - questionList.end() - 2 and randomly pick 3

关于c++ - 总是重复随机选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22314715/

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