当前位置:  开发笔记 > 编程语言 > 正文

if语句和else if语句bouth执行

如何解决《if语句和elseif语句bouth执行》经验,为你挑选了1个好方法。

嘿伙计们,我对编程比较陌生,我正在做刽子手练习.我的问题是在if语句为真后激活后续代码else if 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 <<"test1"; 和cout <<"test2"以确保两个代码块都执行.

不能为我的生活弄清楚为什么if和else一起工作.

完整代码:

#include
#include
#include
#include
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 << "\t\tHangman 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 << "\n\n\n\n\n\n\n";


    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 << "\n\n\n\n\n\n\n";
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    |   0\n    |\n    |\n    |\n    |\n    |\n    |\n----------\n";
hang_pic[2] = "    -----\n    |   |\n    |   0\n    |  -+-\n    |\n    |\n   |\n    |\n    |\n----------\n";
hang_pic[3] = "    -----\n    |   |\n    |   0\n    | /-+-\n    | |\n    |\n    |\n    |\n    |\n----------\n";
hang_pic[4] = "    -----\n    |   |\n    |   0\n    | /-+-\\\n    | |   |\n    |\n    |\n    |\n    |\n----------\n";
hang_pic[5] = "    -----\n    |   |\n    |   0\n    | /-+-\\\n    | | | |\n    |   |\n    |\n    |\n    |\n----------\n";
hang_pic[6] = "    -----\n    |   |\n    |   0\n    | /-+-\\\n    | | | |\n    |   |\n    |  |\n    |  |\n    |\n----------\n";
hang_pic[7] = "    -----\n    |   |\n    |   0\n    | /-+-\\\n    | | | |\n    |   |\n    |  | |\n    |  | |\n    |\n----------\n";

return hang_pic[guess_num];
}

Karoly Horva.. 6

它们都可以被执行,只要不是在同一时间.

加:

cout << i << endl;

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


这是初学者的一个常见错误:你完全意识到它永远不会发生,但你认为这是实际发生的事情.编程的第一条规则:永远是你的错.



1> Karoly Horva..:

它们都可以被执行,只要不是在同一时间.

加:

cout << i << endl;

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


这是初学者的一个常见错误:你完全意识到它永远不会发生,但你认为这是实际发生的事情.编程的第一条规则:永远是你的错.

推荐阅读
臭小子
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有