C++开发:调用指定字符串数组元素时如何递增int变量?
解决DJ选择对应流派分数递增的问题
当然可以实现!你当前的代码问题在于没有把DJ和他们对应的流派建立关联,而且注释里的尝试写法不符合C++语法(比如直接把字符串作为if条件、for循环格式错误)。下面我会给你一个清晰、可扩展的解决方案:
核心思路
- 把DJ的名字和他们的主打流派绑定在一起(用结构体实现,方便后续扩展)
- 建立流派名称到分数变量的映射,这样可以通过流派名直接找到对应的分数并递增
- 根据用户的选择,遍历选中DJ的流派列表,给对应的分数加1
修改后的完整代码
#include "stdafx.h" #include <iostream> #include <string> #include <vector> #include <map> using namespace std; // 定义DJ结构体,绑定名字和对应的流派 struct DJ { string name; vector<string> genres; }; int main() { cout << "Hello! This program is designed to figure out what Electronic Music you like based on artists presented and the answers you choose...\n" << endl; cout << "When you are ready to begin press \"Enter\"..." << endl; getchar(); // 初始化所有流派的分数,初始值为0 int bigRoom = 0; int deepHouse = 0; int drumBass = 0; int dubstep = 0; int electroHouse = 0; int futureHouse = 0; int hardDance = 0; int house = 0; int progressiveHouse = 0; int techno = 0; int trance = 0; int trap = 0; // 建立流派名称到分数变量的映射,方便通过字符串修改分数 map<string, int&> genreScoreMap = { {"Big Room", bigRoom}, {"Deep House", deepHouse}, {"Drum & Bass", drumBass}, {"Dubstep", dubstep}, {"Electro House", electroHouse}, {"Future House", futureHouse}, {"Hard Dance", hardDance}, {"House", house}, {"Progressive House", progressiveHouse}, {"Techno", techno}, {"Trance", trance}, {"Trap", trap} }; // 初始化DJ列表,每个DJ对应他们的主打流派 vector<DJ> djList = { {"DeadMau5", {"Progressive House", "Electro House", "Techno"}}, {"Armin Van Buuren", {"Trance", "Progressive House", "Uplifting Trance"}}, {"Avicii", {"Progressive House", "House", "Folk House"}}, {"Ferry Corsten", {"Trance", "Progressive House", "Electro House"}}, {"Kaskade", {"House", "Deep House", "Progressive House"}} }; string userAnswer; cout << "Select the DJ you prefer by number. Otherwise select 3 if you don't know them. " << endl; // 暂时展示第2和第3位DJ(索引1和2),后续可以改成随机选择 cout << "1 - " << djList[1].name << endl; cout << "2 - " << djList[2].name << endl; cin >> userAnswer; // 根据用户选择处理分数递增 if (userAnswer == "1") { cout << "You have selected: " << djList[1].name << endl; // 遍历该DJ的所有流派,给对应分数加1 for (const string& genre : djList[1].genres) { // 检查流派是否在映射中,避免无效流派导致错误 if (genreScoreMap.find(genre) != genreScoreMap.end()) { genreScoreMap[genre]++; } } } else if (userAnswer == "2") { cout << "You have selected: " << djList[2].name << endl; for (const string& genre : djList[2].genres) { if (genreScoreMap.find(genre) != genreScoreMap.end()) { genreScoreMap[genre]++; } } } else if (userAnswer == "3") { cout << "You chose not to select either DJ." << endl; } else { cout << "Invalid selection! Please try again." << endl; } // 测试输出分数,验证功能是否正常 cout << "\nCurrent Genre Scores:" << endl; cout << "Trance: " << trance << endl; cout << "Progressive House: " << progressiveHouse << endl; cout << "House: " << house << endl; return 0; }
关键改进点说明
- 结构体绑定DJ与流派:用
DJ结构体把每个DJ的名字和流派列表关联,后续添加新DJ只需在djList里新增元素,不用修改核心逻辑。 - 流派映射表:
genreScoreMap让我们可以通过流派字符串直接找到对应的分数变量,避免写大量重复的if-else判断。 - 健壮性处理:增加了对无效输入和"都不选"选项的处理,同时检查流派是否存在于映射表中,避免程序崩溃。
后续随机选择DJ的扩展
你提到的随机选择DJ功能很容易实现,只需要用C++的<random>库生成两个不同的随机索引,然后展示对应的DJ即可,比如:
#include <random> // ... random_device rd; mt19937 gen(rd()); uniform_int_distribution<> dist(0, djList.size()-1); int djIndex1 = dist(gen); int djIndex2 = dist(gen); // 确保两个DJ不重复 while (djIndex2 == djIndex1) { djIndex2 = dist(gen); } // 展示随机选中的DJ cout << "1 - " << djList[djIndex1].name << endl; cout << "2 - " << djList[djIndex2].name << endl;
内容的提问来源于stack exchange,提问作者Matt
相关产品推荐
相关产品推荐

