我是一个c++初学者。我只想检查学生对一组答案的答案。假设我有一个学生,并像这样声明:
//Declare and array of student answers;
string student1 [] = {"A", "B", "C", "A", "B","C","A","B","A","A"};答案关键字声明如下:
//Declare an array set of answer key
string keys [] = {"A", "B", "C", "A", "B","C","A","B","A","A"};假设答案是从1到10的正确答案。然后我想检查学生的答案是否与声明的答案匹配:
for(const string &key : keys){
for(const string &answer : answers){
if(key == answer){
cout << "Correct" << endl;
}else{
cout << "Wrong" << endl;
}
}
}我的第一个问题是,它给了我以下结果:
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct 第二个问题是,我想再增加5个学生来检查他们的答案。谢谢。
发布于 2019-11-03 01:24:47
试着把它变成一个循环:
for(int i = 0;i < 10;i++)
{
if (student1[i] == keys[i])
{
cout << "Correct" << endl;
}
else
{
cout << "Wrong" << endl;
}
}对于第二个,只需创建一个学生数组:
string students[5][10] = {{...},{...},{...},{...},{...}};添加新循环:
for (int j = 0; j < 5; j++)并更改if语句:
if (students[j][i] == keys[i])我希望你能理解。
发布于 2019-11-03 01:41:35
让我先检查一下你想要的东西是否正确。你有一组学生和一组正确答案的测试。每个学生都有一个特定的答案列表。你所要做的就是将学生的答案与关键答案或正确答案进行比较。
让我们从示例中的student1开始。我们有十个问题,student1的所有答案都是正确的,那么为什么你的代码显示了十多行呢?
您创建了两个for循环,而您只需要一个。
你可以这样做:
for (int i=0;i<10;i++) {
if (student1[i] == keys[i]) {
std::cout << "Correct " <<student1[i]<<" "<< keys[i]<< std::endl;
}else std::cout<< "Wrong" <<std::endl;
}您只需将第一个数组中的答案i (student1)与第二个数组中相同位置i的键答案(键)进行比较。您不需要创建两个for循环,除非我确实理解了一些错误。
关于你的第二个问题,这是一个模糊的问题。根据你当前的C/C++水平,你会有很多这样的代码。
如果你想添加更多的学生,你可以简单地声明更多的学生,就像你对student1所做的那样:
std::string student1[] = { "A", "B", "C", "A", "B","C","A","B","A","A" };
std::string student2[] = { "A", "B", "C", "A", "C","C","A","B","A","A" };
std::string student3[] = { "A", "B", "C", "A", "C","C","A","B","A","A" };
//...然后,您可以将数组数组定义为学生列表。
string list[3][10] = {student1,student2,student3};这是一段代码,测试了三个学生的结果:
#include <iostream>
#include <string>
#include<vector>
using namespace std;
void showResult(const string student1[],const string keys[]){
for (int i=0;i<10;i++) {
if (student1[i] == keys[i]) {
std::cout << "Correct " << std::endl;
}else std::cout<< "Wrong" <<std::endl;
}
}
int main(int argc, char** argv) {
std::string student1[] = { "A", "B", "C", "A", "B","C","A","B","A","A" };
std::string student2[] = { "A", "B", "C", "A", "C","C","A","B","A","A" };
std::string student3[] = { "A", "B", "C", "A", "C","C","A","B","A","A" };
string list[3][10] = {student1,student2,student3};
std::string keys[] = { "A", "B", "C", "A", "B","C","A","B","A","A" };
for(int i=0;i<3;i++){
cout<<"student "<<i+1<<endl;
for(int j=0;j<10;j++) cout<<list[i][j]<<" ";
cout<<endl;
showResult(list[i],keys);
cout<<endl;
}
return 0;
}否则,如果你想要更动态的东西,你应该尝试std::vector中的Vectors。C++中的向量比传统数组的功能要多得多。您可以在此线程中阅读有关此主题的更多信息:
发布于 2019-11-03 04:27:58
您可以使用for循环来实现这一点,其他人已经展示了这一点,但是您也可以使用std::transform和lambda来实现这一点,只是为了好玩。
#include <algorithm>
[...]
std::vector<string> correctness;
std::transform(student1,student1+10,keys,std::back_inserter(v),[](const string& a,const string& b){if (a==b) return "Correct"; else return "Wrong";});
for (const auto& ans : correctness) std::cout << ans << endl;https://stackoverflow.com/questions/58673099
复制相似问题