我需要在输出之间留有空格_ 我几乎完成了我的第一个C++项目,它是一个刽子手游戏,到目前为止,一切都很好。唯一的问题是,我需要在表示隐藏单词的下划线之间留有空格。如果有人能在这方面帮助我,我将非常感激 // UNCOMMENT THE FOLLOWING LINE (REMOVE THE TWO SLASHES AT THE BEGINNING) TO RUN AUTOMATIC TESTS #include "tests.h" #include <iostream> #include <string> #include "hangman.h" int main(){ using namespace std; // display the hidden word std::string word_to_guess = chooseWord(); int misses = 0; std::string displayed_word = word_to_guess; for(int i=0; i< displayed_word.length(); i++) displayed_word[i] = '_'; int attempts = 6; std::cout << "Attempts left:" << attempts << std::endl; std::cout << "[ " << displayed_word << " ]" << std::endl; //check for correct letter while(1){ std::cout << "Your guess"; std::cout << ":"; char guess; std::cin >> guess; bool Correct = false; for(int i=0; i< word_to_guess.length(); i++) if (guess == word_to_guess[i]) { displayed_word[i] = word_to_guess[i]; Correct = true; } if (!Correct) attempts--; if (!Correct) std::cout << "Attempts left:" << attempts << std::endl; if (!Correct) std::cout << "[ " << displayed_word << " ]" << std::endl; if (Correct) std::cout << "Attempts left:" << attempts << std::endl; if (Correct) std::cout << "[ " << displayed_word << " ]" << std::endl; //check for win or lose if (attempts==0) std::cout << "The word was: " << word_to_guess << std::endl << "You lost!"; if (attempts==0) return 0; if (!word_to_guess.find(displayed_word)) std::cout << "You won!"; if (!word_to_guess.find(displayed_word)) return 0; } }

我需要在输出之间留有空格_ 我几乎完成了我的第一个C++项目,它是一个刽子手游戏,到目前为止,一切都很好。唯一的问题是,我需要在表示隐藏单词的下划线之间留有空格。如果有人能在这方面帮助我,我将非常感激 // UNCOMMENT THE FOLLOWING LINE (REMOVE THE TWO SLASHES AT THE BEGINNING) TO RUN AUTOMATIC TESTS #include "tests.h" #include <iostream> #include <string> #include "hangman.h" int main(){ using namespace std; // display the hidden word std::string word_to_guess = chooseWord(); int misses = 0; std::string displayed_word = word_to_guess; for(int i=0; i< displayed_word.length(); i++) displayed_word[i] = '_'; int attempts = 6; std::cout << "Attempts left:" << attempts << std::endl; std::cout << "[ " << displayed_word << " ]" << std::endl; //check for correct letter while(1){ std::cout << "Your guess"; std::cout << ":"; char guess; std::cin >> guess; bool Correct = false; for(int i=0; i< word_to_guess.length(); i++) if (guess == word_to_guess[i]) { displayed_word[i] = word_to_guess[i]; Correct = true; } if (!Correct) attempts--; if (!Correct) std::cout << "Attempts left:" << attempts << std::endl; if (!Correct) std::cout << "[ " << displayed_word << " ]" << std::endl; if (Correct) std::cout << "Attempts left:" << attempts << std::endl; if (Correct) std::cout << "[ " << displayed_word << " ]" << std::endl; //check for win or lose if (attempts==0) std::cout << "The word was: " << word_to_guess << std::endl << "You lost!"; if (attempts==0) return 0; if (!word_to_guess.find(displayed_word)) std::cout << "You won!"; if (!word_to_guess.find(displayed_word)) return 0; } },c++,C++,首先,您可以简化此过程 if (!Correct) std::cout << "Attempts left:" << attempts << std::endl; if (!Correct) std::cout << "[ " << displayed_word << " ]" << std::endl; if (Correct) std::cout <&

首先,您可以简化此过程

if (!Correct)    
    std::cout << "Attempts left:" << attempts << std::endl;
if (!Correct)    
    std::cout << "[ " << displayed_word << " ]" << std::endl; 

if (Correct)    
    std::cout << "Attempts left:" << attempts << std::endl;
if (Correct)        
    std::cout << "[ " << displayed_word << " ]" << std::endl; 
由此

std::cout << "Attempts left:" << attempts << std::endl;
std::cout << "[ " << displayed_word << " ]" << std::endl; 
std::cout << "[";
for(int i = 0; i < displayed_word.length(); i++) {
    if(i == 0 || displayed_word[i] == '_')
        std::cout << " ";
    std::cout << displayed_word[i];
    if(i == displayed_word.length()-1 || (displayed_word[i] == '_' && displayed_word[i+1] != '_'))
        std::cout << " ";
}
std::cout << "]" << std::endl;
现在,关于你的问题,我认为最好的解决办法是替换

std::cout << "[ " << displayed_word << " ]" << std::endl;
由此

std::cout << "Attempts left:" << attempts << std::endl;
std::cout << "[ " << displayed_word << " ]" << std::endl; 
std::cout << "[";
for(int i = 0; i < displayed_word.length(); i++) {
    if(i == 0 || displayed_word[i] == '_')
        std::cout << " ";
    std::cout << displayed_word[i];
    if(i == displayed_word.length()-1 || (displayed_word[i] == '_' && displayed_word[i+1] != '_'))
        std::cout << " ";
}
std::cout << "]" << std::endl;
解释:


我们在开始和结束处以及下划线周围放置空格,但我们确保在两个下划线之间仅放置一个空格。

所以您希望用户能够计算下划线?不幸的是,据我所知,你不能这样做,因为在一个普通的控制台中,所有的字符都是相同的大小,你能得到的最小间隙是一个空格。尝试使用其他字符而不是下划线,看看哪一个看起来最好。我进行了编辑,请确保尝试使用我的代码的新版本。