为什么我的while参数没有得到确认

时间:2015-03-15 07:14:07

标签: c++ if-statement parameters do-while

Hangman游戏

当满足参数时,它不会停止循环 有没有不同的方法来做到这一点 或者我编码错了 抱歉,我是一周前开始编码的菜鸟 提前谢谢

这是我的代码:

#include <iostream>
#include <string>


using namespace std;

int main()
{

string sword = "---";
string swordans = "nhs";
char guess;
int guessnum;
guessnum = 0;
int incguessnum;
incguessnum = 0;


while ((((guessnum < 7) && (incguessnum < 6)) || **(sword == swordans)**))
{
    cout<<"Please guess a letter"<<endl;           (   /\  )
    cin>>guess;                                    (   ||  ) 
                                                   (   ||  ) 
    if (guess == 'n')                          (pver here it is not stopping
    {                                           the loop when the replaced
        guessnum ++;                            sword = swordans string)

        cout<<"You have guessed "<< guessnum <<" time(s)"<< endl;
        cout<<"Correct"<<endl;
        sword.replace(0,1,"n");
        cout<<sword<<endl;
    }

    else if (guess == 'h')
    {
        guessnum ++;
        cout<<"You have guessed "<< guessnum <<" time(s)"<< endl;
        cout<<"Correct"<<endl;
        sword.replace(1,1,"h");
        cout<<sword<<endl;
    }
    else if (guess == 's')
    {
        guessnum ++;
        cout<<"You have guessed "<< guessnum <<" time(s)"<< endl;
        cout<<"Correct"<<endl;
        sword.replace(2,1,"s");
        cout<<sword<<endl;
    }
    else 
    {
        guessnum ++;
        cout<<"You have guessed "<< guessnum <<" time(s)"<< endl;
        incguessnum++;
        cout<<"Incorrect"<<endl;
        if (incguessnum == 1)
        {
            cout<<" o"<<endl;
        }
        else if (incguessnum == 2)
        {
            cout<<" o"<<endl;
            cout<<" |"<<endl;
        }
        else if (incguessnum == 3)
        {
            cout<<" o"<<endl;
            cout<<" |"<<endl;
            cout<<" |"<<endl;
        }else if (incguessnum == 4)
        {
            cout<<" o"<<endl;
            cout<<" |"<<endl;
            cout<<" |"<<endl;
            cout<<"/"<<endl;
        }else if (incguessnum == 5)
        {
            cout<<" o"<<endl;
            cout<<" |"<<endl;
            cout<<" |"<<endl;
            cout<<"/ \\"<<endl;
        }else if (incguessnum == 6)
        {
            cout<<"  o"<<endl;
            cout<<"  |"<<endl;
            cout<<"--|"<<endl;
            cout<<" / \\"<<endl;

        }
        else 
        {
            cout<<"  o"<<endl;
            cout<<"  |"<<endl;
            cout<<"--|--"<<endl;
            cout<<" / \\"<<endl;
        }

    }
}


system ("pause");
return(0);
}

2 个答案:

答案 0 :(得分:2)

while ((((guessnum < 7) && (incguessnum < 6)) || (sword == swordans)))

此循环将继续,同时猜测次数小于7且错误猜测小于6.当剑等于剑侠时,它将继续。

您可以将其替换为所需效果:

while(guessnum < 7 && incguessnum < 6 && sword != swordans)

它也读得更整齐;)

答案 1 :(得分:1)

如果目标是在sword == swordans时终止循环,则需要将条件调整为&& (sword != swordans)。你只想继续循环,如果玩家没有用完他们所有的猜测并且他们还没有解决这个难题。

就目前而言,一旦字符串相等,你的循环将无限延续 - 肯定不是你想要的。

您的情况正在被承认&#34;准确,但由于条件错误,你不会看到所期望的行为。