C++ 循环增量不工作
我是一个编程新手,也是这个论坛的新手。我一直在做一个开始的项目,一个关于刽子手的游戏。我使用一个迭代常数numberguesss来跟踪用户输入了多少猜测。然而,我的程序没有正确地迭代,我无法理解为什么会这样。非常感谢您的帮助C++ 循环增量不工作,c++,loops,iteration,C++,Loops,Iteration,我是一个编程新手,也是这个论坛的新手。我一直在做一个开始的项目,一个关于刽子手的游戏。我使用一个迭代常数numberguesss来跟踪用户输入了多少猜测。然而,我的程序没有正确地迭代,我无法理解为什么会这样。非常感谢您的帮助 #include <iostream> #include <string> using namespace std; int main() { string userWord = " "; char userLetter = ' '
#include <iostream>
#include <string>
using namespace std;
int main()
{
string userWord = " ";
char userLetter = ' ';
int numberGuesses = 0;
string board1 =
" -------|\n"
" | |\n"
" |\n"
" |\n"
" |\n"
" |\n"
" -----";
string board2 =
" -------|\n"
" | |\n"
" O |\n"
" |\n"
" |\n"
" |\n"
" -----";
string board3 =
" -------|\n"
" | |\n"
" O |\n"
" | |\n"
" |\n"
" |\n"
" -----";
string board4 =
" -------|\n"
" | |\n"
" O |\n"
"-| |\n"
" |\n"
" |\n"
" -----";
string board5 =
" -------|\n"
" | |\n"
" O |\n"
"-|- |\n"
" |\n"
" |\n"
" -----";
string board6 =
" -------|\n"
" | |\n"
" O |\n"
"-|- |\n"
"/ |\n"
" |\n"
" -----";
string board7 =
" -------|\n"
" | |\n"
" O |\n"
"-|- |\n"
"/ \\ |\n"
" |\n"
" -----";
cout << "Enter a word to guess: ";
cin >> userWord;
for (int i = 0; i < userWord.length(); i++)
{
userWord[i] = toupper(userWord[i]);
}
cout << "You entered: " << userWord << endl;
do
{
if (numberGuesses = 0)
{
cout << board1;
}
if (numberGuesses = 1)
{
cout << board2;
}
else if (numberGuesses = 2)
{
cout << board3;
}
else if (numberGuesses = 3)
{
cout << board4;
}
else if (numberGuesses = 4)
{
cout << board5;
}
else if (numberGuesses = 5)
{
cout << board6;
}
else if (numberGuesses = 6)
{
cout << board7;
}
cout << "\nEnter a letter to guess: ";
cin >> userLetter;
userLetter = toupper(userLetter);
cout << "You entered: " << userLetter << endl;
if (userWord.find(userLetter) != string::npos)
cout << userLetter << " is in the word to guess." << endl;
else
cout << userLetter << " is NOT in the word to guess." << endl;
numberGuesses++;
}
while (numberGuesses <= 6);
return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
字符串userWord=“”;
字符userLetter='';
int numberguesss=0;
线板1=
“——————\n”
“| | \n”
“|\n”
“|\n”
“|\n”
“|\n”
" -----";
字符串board2=
“——————\n”
“| | \n”
“O |\n”
“|\n”
“|\n”
“|\n”
" -----";
字符串board3=
“——————\n”
“| | \n”
“O |\n”
“| | \n”
“|\n”
“|\n”
" -----";
字符串board4=
“——————\n”
“| | \n”
“O |\n”
“-| | \n”
“|\n”
“|\n”
" -----";
字符串板5=
“——————\n”
“| | \n”
“O |\n”
“-|-| \n”
“|\n”
“|\n”
" -----";
字符串板6=
“——————\n”
“| | \n”
“O |\n”
“-|-| \n”
“/|\n”
“|\n”
" -----";
字符串board7=
“——————\n”
“| | \n”
“O |\n”
“-|-| \n”
“/\\\\n”
“|\n”
" -----";
cout>userWord;
for(int i=0;i cout=
是赋值运算符。要检查是否相等,应使用=
运算符:
if (numberGuesses == 0) {
// Here -------^
// (and do the same for the other conditions)
通过使用board
s(或字符串)数组,可以消除大量if
语句