C++ 具有for循环的goto()函数

C++ 具有for循环的goto()函数,c++,arrays,loops,C++,Arrays,Loops,我正在做一个简单的猜测游戏,用户必须猜测隐藏地图(数组)中的一个关键点位置,他有12次尝试进行猜测 我几乎成功了,但唯一的问题是计数器没有改变,我希望TryCounter在用户猜不到钥匙位置时,实际计算尝试次数 这是我的代码: #include <iostream> using namespace std; int main() { bool w; int x; int y; int array1[6][6] = {0,0,0,0,0,0

我正在做一个简单的猜测游戏,用户必须猜测隐藏地图(数组)中的一个关键点位置,他有12次尝试进行猜测 我几乎成功了,但唯一的问题是计数器没有改变,我希望
TryCounter
在用户猜不到钥匙位置时,实际计算尝试次数 这是我的代码:

#include <iostream> 
using namespace std;

int main()
{
    bool w;
    int x;
    int y;
    
    int array1[6][6] = {0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,0,0,0};
    cout << "game is starting ... " << endl;
    cout << "You have 12 try to find one of the hidden keys " << endl;
    loop: for (int TryCounter = 1; TryCounter <= 12; TryCounter++)
    {
        cout << " guess  " << TryCounter << "  - x and -y coordiants : " << endl;
        cin >> x;
        cin >> y;
         if (TryCounter == 12)
        {
            cout << "You have used your chances" << endl;
            if (w == false)
            {
                cout << "You lost" << endl;
                for (int i = 0; i < 6; i++)//Drawing the hidden array
                {
                    cout << "   " << endl;
                    for (int j = 0; j < 6; j++)
                    {
                        cout << array1[i][j];
                        cout << "   ";  
                    }
                }break;
            }break;
    }
        for (int i = 0; i < 6; i++)//checking the input
        {
            for (int j = 0; j < 6; j++)
            {
                if (array1[x][y] == 1)
                {
                    cout << "" << endl;
                    cout << "--- Nice Shot ! ---" << endl;
                    w = true;
                    cout << "You Won" << endl;
                    for (int i = 0; i < 6; i++) //drwaing the hidden map
                    {
                        cout << "   " << endl;
                        for (int j = 0; j < 6; j++)
                        {
                            cout << array1[i][j];
                            cout << "   ";
                        }
                    }return 0;
                }
                else if (array1[x][y] == 0) //wrong guess
                {
                    w = false;
                    cout << "--- You missed ---" << endl;
                    goto loop ;
                }
            }
        } 
    }

}
 
#包括
使用名称空间std;
int main()
{
布尔w;
int x;
int-y;
int array1[6][6]={0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0};

cout计数器不会更改,因为您使用了
goto
。计数器在循环结束时更改(右括号)。您的执行指针永远不会到达右大括号,因为
goto
会更早地将其引导到循环的开头。

通常人们会在没有goto的情况下争先恐后地编写程序。但有时它确实很有用……所以这里没有判断

int main(...) {
   // the variable and array stuff...
   for(int attemptCounter = 0; attemptCounter < 12; ++attemptCounter) {
      // the user input stuff...
      // the checking if user guessed right
      if( userGuessedCorrectly ) goto Success;
   }
   std::cout << "better luck next time, pal - you did not guess right." << std::endl;
   return 0;
Success:
   std::cout << "Congratulations you made it!" << std::endl;
   return 0;

}
int main(…){
//变量和数组的东西。。。
对于(int attemptCounter=0;attemptCounter<12;++attemptCounter){
//用户输入的东西。。。
//检查用户是否猜对了
如果(用户猜对了)成功了;
}

看看你的代码,它看起来很复杂。 我试图通过一些重构来理解它,我希望你已经有了一些bug

我的建议是从这段代码中提取一些函数。 我已经识别了一些重复的代码来将数组打印到std::cout。 这将在最深的点上将循环嵌套减少2

第二个功能是检查功能,检查快照是否正常。 在这里,我注意到在循环中,您很可能对数组进行了错误的索引,因为您的循环使用I和j(在循环中未使用),而您使用x和y进行索引

提取后,您将得到一个for循环,您可以使用相同名称的关键字继续/中断该循环


我的经验法则是,如果您需要记住for循环的状态,那么应该将它们放入函数中。

TryCounter
在goto标签之后初始化。在
循环:
之前移动
int-TryCounter=1
。没有“goto函数”在C++中,<>代码> Goto 是一个语言关键字,它被用于GOTO语句。我应该在哪里放置它?我试过几个位置,但没有一个是有效的。i486@Peter对不起,我不知道我在自学编程。不要投反对票。这是一个合理的问题,IMHO。答案是,你可以删除循环标签d goto。就这么简单。然后,让for循环包含带有检查的块。并删除
if(TryCounter==12)
检查。for循环已经为您完成了这项工作。编写此循环时不需要
转到
。使用
转到
会混淆实际发生的情况。使用
转到
通常被认为是有害的,就像使用原始拥有指针被认为是有害的一样。这不是“不判断”的情况好像这是个人的选择。我们可以批评不好的纪律而不“评判”在C++中,对于使用代码< > Goto < /Cord>,有一些很好的理由:在C++中使用<代码> Goto < /Cord>没有什么好的理由。@ CDHOWIE取决于您所应用的度量。在没有这种情况下,当然我同意没有人会认真使用GOTO,您需要将for循环的第一个词移到外部和以后添加一个
if(attemptCounter<12){…}else{…}
,如果你对它应用“循环复杂度”度量,它比goto更复杂。只是说…不是每个goto都是黑色的,不是所有其他的都是白色的。;)什么?你可以用
替换
goto Success;
{std::我不能感谢您的重播,与您的代码相比,我的代码真是一团糟。@JVApen Edit:我运行的代码看起来像
,如果(array1[x][y]==0)//猜错了{w=false;正如我所说的,我必须更改一些东西,因为我认为您在其中有一个bug。但是,我认为总体要点很清楚:在一个函数中放入一些东西,以降低另一个函数的复杂性,从而使最终结果更易于维护