我一般都不熟悉C ++和编程。我正在做一个琐事游戏,我想随机分配我的问题,以便每次有人玩时问题的顺序都不同。只是不确定如何去做。感谢您的帮助。
答案 0 :(得分:0)
为此,您可以将问题和答案组合在一个结构中,以使它们在一起,然后将一堆问题和答案存储在某种数据结构中(例如std::shuffle
)。然后,您可以使用// question and answer pairs
struct question_info
{
std::string Q;
std::string A;
};
// data structure to store your questions in memory
std::vector<question_info> questions;
// insert some questions (or load them from a file)
questions.push_back({"Why did the chicken cross the road?", "To get to the other side."});
questions.push_back({"Why did the fox cross the road?", "To eat the chicken."});
questions.push_back({"Why did the lemmings cross the road?", "The cliff was on the other side."});
// instantiate a pseudo random number generator
std::mt19937 prng{std::random_device{}()};
// randomly shuffle the questions
std::shuffle(std::begin(questions), std::end(questions), prng);
// print them
for(auto item: questions)
{
std::cout << "Q: " << item.Q << '\n';
std::cout << "A: " << item.A << '\n';
std::cout << '\n';
}
随机重新排列它们:
{{1}}