代码很丑陋,应该构成一个简单的测试,但是存在一些问题。.重点是创建一个包含十个问题的问答游戏,其目的是选择并选择带有箭头的答案并输入。我希望提出一些建议或改进。
在单击“输入”按钮后,如何重新构造它以继续进行下一个问题。.到目前为止,只有一个问题可用,它检查是否正确,并将点+1添加到变量“ d”中。
知识水平:高中
#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
int main()
{ char k;
int m = 0, d = 0 ;
const int Up = 72;
const int Down = 80;
const int Enter = 13;
cout<<" 1.Which is the closest planet to the Sun?\n"
"> A) Mercury\n"
" B) Mars\n"
" C) Earth\n"
" D) Neptune\n";
do {
k = getch();
if(k == Down) {
m++;
}
else if(k == Up) {
m--;
}
if(m>3) {
m = 0;
}
else if(m < 0) {
m = 3;
}
system("CLS");;
if (m == 0) {
cout << " 1.What is the closest planet to the Sun?\n"
"> A) Mercury\n";
}
else {
cout<<" 1.What is the closest planet to the Sun?\n"
" A) Mercury\n";
}
if (m == 1) {
cout << "> B) Mars\n";
}
else {
cout<< " B) Mars\n";
}
if (m == 2) {
cout << "> C) Earth\n";
}
else {
cout<< " C) Earth\n";
}
if (m == 3) {
cout << "> D) Neptune\n\n";
}
else {
cout<< " D) Neptune\n\n";
}
} while (k != Enter);
if (m==0) {
d++;
}
}
答案 0 :(得分:1)
定义代表问题(及其答案)的数据结构,并创建此类结构的集合。
然后,您可以遍历它们,也可以随意选择其中之一。
例如:
#include <iostream>
#include <string>
#include <vector>
struct Question
{
std::string question;
std::vector<std::string> answers;
int correct;
};
std::vector<Question> questions =
{
{ "What is the moon", {"Cheese", "Fake"}, 0 },
{ "Where's Waldo", {"Over here", "Over there"}, 1}
};
int main()
{
for (const auto& q: questions)
{
std::cout << q.question << "?\n";
char option = 'A';
for (const auto& a: q.answers)
{
std::cout << option << ") " << a << '\n';
option += 1;
}
std::cout << "Correct answer: " << q.answers[q.correct] << "\n\n";
}
}
输出:
What is the moon?
A) Cheese
B) Fake
Correct answer: Cheese
Where's Waldo?
A) Over here
B) Over there
Correct answer: Over there