If语句和else If语句将执行
If statement and and else if statement bouth execute
嘿,伙计们,我对编程还比较陌生,我正在做刽子手练习。我的问题是在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;
在循环体的顶部,以更好地了解情况。
这在初学者中是一个常见的错误:你完全意识到这不应该发生,但你认为这就是实际发生的事情。编程的第一条规则:总是你的错。
相关文章:
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 以在Qt中的IF语句中设置时间延迟
- Craps游戏问题,忽略if语句
- c++:定义if语句中的模板
- C++嵌套的 If 语句中,它无法按预期工作
- 如果条件不相关,我应该更喜欢两个 if 语句而不是 if-else 语句吗?
- 需要有关此 if 语句的帮助
- 转换器练习:跳过 if 语句和 if 语句
- 避免在 C++ 中的 if 语句中递增
- 如果仍然不满足要求,如何使 if 语句重复?
- do while 循环中的 if 语句以 yes 或 no 结尾
- 为什么在此C++代码中触发此 if 语句?
- 如何基于平台在制作文件中制作if语句?
- 在 if 语句中使用 GetKeyState 时,它会跳过条件
- C++ If 语句,从其他语句重复