代码不';我不工作,我也不';我不知道为什么。制作刽子手游戏 我对C++和编码是新的。我试着做一个刽子手游戏作为一个初学者项目。我遇到的问题是,只有按顺序键入单词的字母时,游戏才起作用。例如,如果单词是“flow”,我必须连续键入每个字母(f、l、o、w)。不接受任何其他变更,我不知道为什么。我需要调试此问题的帮助。我不确定我在这里是否应该使用.replace。我在互联网上找到了这个方法,我认为它可以满足我的需要 #include <iostream> #include <string> #include <time.h> #include <stdlib.h> using namespace std; string getString(char guess) { string s(1, guess); return s; } int main() { unsigned int seed; int randomNumber = 0; char guess; string underscore; seed = time(0); cout << "Hangman game\n"; srand(seed); randomNumber = (rand() % 5); string wordList[5] = {"closet", "flow", "sheep", "see", "chocolate"}; string word = wordList[randomNumber]; int wordLength = word.length(); cout << "The word has " << wordLength << " letters\n"; for (int x = 0; x < wordLength; x++) { underscore += "_ "; } cout << underscore << endl; string holder = underscore; for (int j = 0; j < wordLength; j++) { cout << "\n\nType in a letter: "; cin >> guess; if (guess == word[j]) { size_t found = word.find(guess); holder.replace(found, 2, getString(guess)); cout << "\n"; word.replace(found, 1, "*"); cout << holder; } else { } } return 0; } #包括 #包括 #包括 #包括 使用名称空间std; 字符串getString(字符猜测){ 字符串s(1,猜测); 返回s; } int main(){ 无符号整数种子; int随机数=0; 猜字符; 字符串下划线; 种子=时间(0); cout

代码不';我不工作,我也不';我不知道为什么。制作刽子手游戏 我对C++和编码是新的。我试着做一个刽子手游戏作为一个初学者项目。我遇到的问题是,只有按顺序键入单词的字母时,游戏才起作用。例如,如果单词是“flow”,我必须连续键入每个字母(f、l、o、w)。不接受任何其他变更,我不知道为什么。我需要调试此问题的帮助。我不确定我在这里是否应该使用.replace。我在互联网上找到了这个方法,我认为它可以满足我的需要 #include <iostream> #include <string> #include <time.h> #include <stdlib.h> using namespace std; string getString(char guess) { string s(1, guess); return s; } int main() { unsigned int seed; int randomNumber = 0; char guess; string underscore; seed = time(0); cout << "Hangman game\n"; srand(seed); randomNumber = (rand() % 5); string wordList[5] = {"closet", "flow", "sheep", "see", "chocolate"}; string word = wordList[randomNumber]; int wordLength = word.length(); cout << "The word has " << wordLength << " letters\n"; for (int x = 0; x < wordLength; x++) { underscore += "_ "; } cout << underscore << endl; string holder = underscore; for (int j = 0; j < wordLength; j++) { cout << "\n\nType in a letter: "; cin >> guess; if (guess == word[j]) { size_t found = word.find(guess); holder.replace(found, 2, getString(guess)); cout << "\n"; word.replace(found, 1, "*"); cout << holder; } else { } } return 0; } #包括 #包括 #包括 #包括 使用名称空间std; 字符串getString(字符猜测){ 字符串s(1,猜测); 返回s; } int main(){ 无符号整数种子; int随机数=0; 猜字符; 字符串下划线; 种子=时间(0); cout,c++,debugging,C++,Debugging,以下是一些可能对您有所帮助的观察结果: 不要在函数顶部声明所有变量。根据需要声明它们 避免硬编码(单词列表[5])。在数组中添加所需数量的字符串。使用以下命令可确定有多少字符串(请参阅): 您不需要手动填写下划线字符串。请使用以下构造函数: 用户可以输入大写字母。将它们转换为小写。否则,您将找不到它们: 您不需要复杂的函数来找出输入字符的位置。只需使用循环即可: /。。。 布尔发现=真; for(int i=0;i

以下是一些可能对您有所帮助的观察结果:

  • 不要在函数顶部声明所有变量。根据需要声明它们

  • 避免硬编码
    单词列表[5]
    )。在数组中添加所需数量的字符串。使用以下命令可确定有多少字符串(请参阅):

  • 您不需要手动填写
    下划线
    字符串。请使用以下构造函数:
    

  • 用户可以输入大写字母。将它们转换为小写。否则,您将找不到它们:

  • 您不需要复杂的函数来找出输入字符的位置。只需使用循环即可:

    /。。。
    布尔发现=真;
    for(int i=0;i我能感觉到你混合了两件事,尝试的次数和单词的长度。对于第一个方面,最简单的方法似乎是使用
    while(true)
    循环并在找到单词时“中断”。如果在此循环中,我们使用
    for
    循环检查每个字符,则不需要使用
    查找
    替换
    方法。但是,使用它们可以避免
    for
    循环。“在internet上查找”不是使用某个东西的好理由。你需要运用一些科学的、分析性的推理来解释为什么你要写一行特定的代码。谢谢这帮了大忙。游戏现在可以运行了。我只想在下划线之间留一个空格,当你猜对了字母时,以某种方式把它去掉,这样看起来更干净,但这对我来说太难了我想这就是我代码中的问题。谢谢again@Beza以一种非常简单、易于测试和操作的格式存储信息是一个好主意。如果您愿意,可以在显示信息时使其看起来漂亮。但通常以显示格式存储数据不是一个好主意,因为这样会使操作变得更难,也更容易容易出错。(这也使得更改显示格式或尝试不同的方式向用户显示信息变得更加困难。)
    string wordList[] = { "closet", "flow", "sheep", "see", "chocolate" };
    size_t wordCount = sizeof wordList / sizeof wordList[0];
    
    string underscore(word.length(), '_');
    
    guess = tolower(guess);
    
    //...
    
    bool found = true;
    for (int i = 0; i < word.length(); i++)
    {
      if (word[i] == guess) // is the letter at position i the same as the one entered?
        underscore[i] = guess; // replace it
      if (underscore[i] == '_')
        found = false;
    }
    
    if (found)
    {
      cout << "Good job!" << endl;
      return 0;
    }
    
    //...