If语句和else If语句将执行

If statement and and else if statement bouth execute

本文关键字:If 语句 执行 else      更新时间:2023-10-16

嘿,伙计们,我对编程还比较陌生,我正在做刽子手练习。我的问题是在if语句为true并激活后续代码之后,else-if语句也会执行。这是有问题的代码:

    for (int i = 0; i < word.size(); i++) {     //cheks the letters against the control word
        if (p1_input == word[i]) {
            guess[i] = p1_input;
            cout << "TEST 1!!!!" << endl;
        }
        else if (wrong_letters.find(p1_input) == std::string::npos) {
            wrong_letters += " ";
            wrong_letters += p1_input;
            guess_num++;
            cout << "TEST 2!!!!" << endl;
        }
    }
}

iv添加了cout<lt;"test1";并且cout<lt;"test2"以确保两个代码块都执行。

我一辈子都不明白为什么if和else if都在一起工作。

完整代码:

#include<iostream>
#include<string>
#include<ctime>
#include<random>
using namespace std;
string Word_box();
//@keeps an asortment of words and picks a random one from the directory
//@return string- a random word to be used in the game
string Hangman(int);
//stores the hangman pics
//@param int- the amount of wrong guesses
//return string- the coresponding pic
int main() {
string word = Word_box();   //word to be guessed
string guess;               //user gessed word
int guess_num = 0;          //number of guesses
string wrong_letters;       //wrong letters inserted
char p1_input;              //user input
//test1
cout << word << " TEST" << endl;
//creating the guess word
for (int i = 0; i < word.size(); i++) {
    guess += "-";
}
//test 2
cout << guess << endl;
//insructions
cout << "ttHangman v1.0" << endl << endl;
cout << "Lets play a game...nIm thinking of a word...nTry and guess it..." << endl << endl;
//game loop
while ((word.compare(guess) != 0) && (guess_num != 8)) {
    cout << Hangman(guess_num) << endl << endl;
    cout << "tYour wrong guesses:" << endl;
    cout << "t[" << wrong_letters << " ]" << endl << endl;
    cout << "t" << guess << endl << endl;
    cout << "Guess please: ";
    cin >> p1_input;
    cout << "nnnnnnn";

    for (int i = 0; i < word.size(); i++) {     //cheks the letters against the control word
        if (p1_input == word[i]) {
            guess[i] = p1_input;
            cout << "TEST 1!!!!" << endl;
        }
        else if (wrong_letters.find(p1_input) == std::string::npos) {
            wrong_letters += " ";
            wrong_letters += p1_input;
            guess_num++;
            cout << "TEST 2!!!!" << endl;
        }
    }
}
//game over
cout << "nnnnnnn";
cout << "The word was: " << word << endl;
if (word.compare(guess) != 0) {
    cout << Hangman(7) << endl << endl;
    cout << "GAME OVER!" << endl;
}
else {
    cout << Hangman(guess_num) << endl << endl;
    cout << "you win!" << endl;
}
cout << endl;
return 0;
}   //End
string Word_box() {
string game_word[10];                       //the word used in the game
srand(time(0));                             //random number generator
unsigned int rand_word = rand() % 10;       //number between 0-9
game_word[0] = "loot";
game_word[1] = "rnjesus";
game_word[2] = "dps";
game_word[3] = "troll";
game_word[4] = "skill";
game_word[5] = "mushroom";
game_word[6] = "noob";
game_word[7] = "derp";
game_word[8] = "pcmasterrace";
game_word[9] = "headshot";
return game_word[rand_word];
}
string Hangman(int guess_num) {
string hang_pic[8];         //stores the hngman pics
hang_pic[0] = "    -----n    |   |n    |n    |n    |n    |n    |n    |n    |n----------n";;
hang_pic[1] = "    -----n    |   |n    |   0n    |n    |n    |n    |n    |n    |n----------n";
hang_pic[2] = "    -----n    |   |n    |   0n    |  -+-n    |n    |n   |n    |n    |n----------n";
hang_pic[3] = "    -----n    |   |n    |   0n    | /-+-n    | |n    |n    |n    |n    |n----------n";
hang_pic[4] = "    -----n    |   |n    |   0n    | /-+-\n    | |   |n    |n    |n    |n    |n----------n";
hang_pic[5] = "    -----n    |   |n    |   0n    | /-+-\n    | | | |n    |   |n    |n    |n    |n----------n";
hang_pic[6] = "    -----n    |   |n    |   0n    | /-+-\n    | | | |n    |   |n    |  |n    |  |n    |n----------n";
hang_pic[7] = "    -----n    |   |n    |   0n    | /-+-\n    | | | |n    |   |n    |  | |n    |  | |n    |n----------n";
return hang_pic[guess_num];
}

它们都可以执行,只是不能同时执行。

添加:

cout << i << endl;

在循环体的顶部,以更好地了解情况。


这在初学者中是一个常见的错误:你完全意识到这不应该发生,但你认为这就是实际发生的事情。编程的第一条规则:总是你的错。