C++ 在这种情况下我可以使用goto吗?

C++ 在这种情况下我可以使用goto吗?,c++,goto,C++,Goto,我想知道,在这种情况下使用goto是否可以?你能提出更好的解决方案吗?我看到只有一个在cicle的时候做了第二个,但是接下来有必要叫“makeMove”两次 void BoardView::startGame() { int currStep=0; int x,y; while (board_->isWin()==none) { currStep++; show(); wrong: std::cout

我想知道,在这种情况下使用goto是否可以?你能提出更好的解决方案吗?我看到只有一个在cicle的时候做了第二个,但是接下来有必要叫“makeMove”两次

void BoardView::startGame()
{
    int currStep=0;
    int x,y;
    while (board_->isWin()==none)
    {
        currStep++;
        show();
    wrong:
        std::cout << " Player " << (currStep%2==0 ? 1 : 2) << ": ";
        std::cin >> x;
        y=x%10;
        x/=10;
        if (!board_->makeMove(x,y,(currStep%2==0 ? cross : zero)))
        {
            std::cout << "Wrong move! Try again.\n";
            goto wrong;
        }
    }
}
void BoardView::startGame()
{
int currStep=0;
int x,y;
while(board->isWin()==无)
{
currStep++;
show();
错:
std::cout makeMove(x,y,(当前步骤%2==0?交叉:零)))
{

std::cout不要使用
goto
。使用
while(true)
循环,并在成功移动后将
中断

while (true) {
    std::cout << " Player " << (currStep%2==0 ? 1 : 2) << ": ";
    std::cin >> x;
    y=x%10;
    x/=10;
    if (board_->makeMove(x,y,(currStep%2==0 ? cross : zero)))
        break;
    std::cout << "Wrong move! Try again.\n";
}
while(true){
std::cout makeMove(x,y,(当前步骤%2==0?交叉:零)))
打破

std::cout不要使用
goto
。使用
while(true)
循环,并在成功移动后将
中断

while (true) {
    std::cout << " Player " << (currStep%2==0 ? 1 : 2) << ": ";
    std::cin >> x;
    y=x%10;
    x/=10;
    if (board_->makeMove(x,y,(currStep%2==0 ? cross : zero)))
        break;
    std::cout << "Wrong move! Try again.\n";
}
while(true){
std::cout makeMove(x,y,(当前步骤%2==0?交叉:零)))
打破
std::cout可能:

void BoardView::startGame()
{
int currStep=1;
int x,y;
show();
while(board->isWin()==无)
{
std::cout makeMove(x,y,(当前步骤%2==0?交叉:零)))
{
std::cout可能:

void BoardView::startGame()
{
int currStep=1;
int x,y;
show();
while(board->isWin()==无)
{
std::cout makeMove(x,y,(当前步骤%2==0?交叉:零)))
{

std::cout是的,您可以进行这样的跳转,但通常最好避免
跳转
。您可以这样重写它,例如:

void BoardView::startGame()
{
    int currStep=1;
    int x,y;
    show();
    while (board_->isWin()==none)
    {
        std::cout << " Player " << (currStep%2==0 ? 1 : 2) << ": ";
        std::cin >> x;
        y=x%10;
        x/=10;
        if (board_->makeMove(x,y,(currStep%2==0 ? cross : zero)))
        {
            currStep++;
            show();
        }
        else
        {
            std::cout << "Wrong move! Try again.\n";
        }
    }
}
void BoardView::startGame()
{
int currStep=1;
int x,y;
show();
while(board->isWin()==无)
{
std::cout makeMove(x,y,(当前步骤%2==0?交叉:零)))
{
currStep++;
show();
}
其他的
{

std::cout是的,您可以进行这样的跳转,但通常最好避免
跳转
。您可以这样重写它,例如:

void BoardView::startGame()
{
    int currStep=1;
    int x,y;
    show();
    while (board_->isWin()==none)
    {
        std::cout << " Player " << (currStep%2==0 ? 1 : 2) << ": ";
        std::cin >> x;
        y=x%10;
        x/=10;
        if (board_->makeMove(x,y,(currStep%2==0 ? cross : zero)))
        {
            currStep++;
            show();
        }
        else
        {
            std::cout << "Wrong move! Try again.\n";
        }
    }
}
void BoardView::startGame()
{
int currStep=1;
int x,y;
show();
while(board->isWin()==无)
{
std::cout makeMove(x,y,(当前步骤%2==0?交叉:零)))
{
currStep++;
show();
}
其他的
{

std::cout一般建议是避免GOTO语句,但是,请参阅修改后的代码和do while

    void BoardView::startGame()
{
    int currStep=0;
    int x,y;
    while (board_->isWin()==none) {

        currStep++;
        show();
        int retry = 0; /* So that 'retry' is visible to do while loop */ 
        do {
              retry = 0;
              std::cout << " Player " << (currStep%2==0 ? 1 : 2) << ": ";
              std::cin >> x;
              y=x%10;
              x/=10;
             if (!board_->makeMove(x,y,(currStep%2==0 ? cross : zero))) {

                std::cout << "Wrong move! Try again.\n";
                retry = 1
             } 

       } while (retry);
    }
}
void BoardView::startGame()
{
int currStep=0;
int x,y;
while(board->isWin()==无){
currStep++;
show();
int retry=0;/*以便在循环时可以看到“retry”
做{
重试=0;
std::cout makeMove(x,y,(当前步骤%2==0?交叉:零))){

std::cout一般建议是避免GOTO语句,但是,请参阅修改后的代码和do while

    void BoardView::startGame()
{
    int currStep=0;
    int x,y;
    while (board_->isWin()==none) {

        currStep++;
        show();
        int retry = 0; /* So that 'retry' is visible to do while loop */ 
        do {
              retry = 0;
              std::cout << " Player " << (currStep%2==0 ? 1 : 2) << ": ";
              std::cin >> x;
              y=x%10;
              x/=10;
             if (!board_->makeMove(x,y,(currStep%2==0 ? cross : zero))) {

                std::cout << "Wrong move! Try again.\n";
                retry = 1
             } 

       } while (retry);
    }
}
void BoardView::startGame()
{
int currStep=0;
int x,y;
while(board->isWin()==无){
currStep++;
show();
int retry=0;/*以便在循环时可以看到“retry”
做{
重试=0;
std::cout makeMove(x,y,(当前步骤%2==0?交叉:零))){

std::cout您应该尽可能避免goto。仅在大型嵌套程序中使用。否则,使用goto会使程序不可靠、不可读,并且难以调试。 goto的另一个大问题是,当我们使用它们时,我们永远无法确定我们是如何达到代码中的某一点的。它们模糊了控制流。所以要避免它们


我建议使用两个while循环……这样会更好……

您应该尽可能避免转到。仅在大型嵌套程序中使用。否则,使用转到会使程序不可靠、不可读且难以调试。 goto的另一个大问题是,当我们使用它们时,我们永远无法确定我们是如何达到代码中的某一点的。它们模糊了控制流。所以要避免它们

我建议使用两个while循环…这样会更好…

有什么问题:

std::pair<int, int> BoardView::getNextMove()
{
    std::cout << " Player " << (currStep & 2 == 0 ? 1 : 2) << ": ";
    int tmp;
    std::cin >> temp;
    return std::pair<int, int>( tmp / 10, tmp % 10 );
}

void BoardView::startGame() 
{
    int currentStep = 0;
    while ( myBoard->isWin() == none ) {
        std::pair<int, int> move = getNextMove();
        while ( ! myBoard->makeMove( move, (currentStep % 2 == 0 ?  cross : zero) ) {
            std::cout << "Wrong move! Try again" << std::endl;
            move = getNextMove();
        }
    }
}
std::pair BoardView::getNextMove()
{
std::cout isWin()==无){
std::pair move=getNextMove();
而(!myBoard->makeMove(移动,(当前步骤%2==0?交叉:零)){
std::cout有什么问题:

std::pair<int, int> BoardView::getNextMove()
{
    std::cout << " Player " << (currStep & 2 == 0 ? 1 : 2) << ": ";
    int tmp;
    std::cin >> temp;
    return std::pair<int, int>( tmp / 10, tmp % 10 );
}

void BoardView::startGame() 
{
    int currentStep = 0;
    while ( myBoard->isWin() == none ) {
        std::pair<int, int> move = getNextMove();
        while ( ! myBoard->makeMove( move, (currentStep % 2 == 0 ?  cross : zero) ) {
            std::cout << "Wrong move! Try again" << std::endl;
            move = getNextMove();
        }
    }
}
std::pair BoardView::getNextMove()
{
std::cout isWin()==无){
std::pair move=getNextMove();
而(!myBoard->makeMove(移动,(当前步骤%2==0?交叉:零)){

std::cout两个循环,没有常量条件表达式,只有一个对makeMove的调用:

void BoardView::startGameLoop()
{
    int currStep = 0;
    int x,y;
    while (none == board_->isWin())
    {
        ++currStep;
        show();

        for (;;)
        {
            std::cout << " Player " << ((currStep & 1) + 1) << ": ";
            std::cin >> x;
            y = x % 10;
            x /= 10;
            if (!board_->makeMove(x, y, (currStep & 1) ? zero : cross))
            {
                std::cout << "Wrong move! Try again.\n";
                continue;
            }
            break;
        }
    }
}
void BoardView::startGameLoop()
{
int currStep=0;
int x,y;
而(none==board->isWin())
{
++库尔斯特普;
show();
对于(;;)
{
标准:cout makeMove(x,y,(currStep&1)?零:交叉)
{

std::cout两个循环,没有常量条件表达式,只有一个对makeMove的调用:

void BoardView::startGameLoop()
{
    int currStep = 0;
    int x,y;
    while (none == board_->isWin())
    {
        ++currStep;
        show();

        for (;;)
        {
            std::cout << " Player " << ((currStep & 1) + 1) << ": ";
            std::cin >> x;
            y = x % 10;
            x /= 10;
            if (!board_->makeMove(x, y, (currStep & 1) ? zero : cross))
            {
                std::cout << "Wrong move! Try again.\n";
                continue;
            }
            break;
        }
    }
}
void BoardView::startGameLoop()
{
int currStep=0;
int x,y;
而(none==board->isWin())
{
++库尔斯特普;
show();
对于(;;)
{
标准:cout makeMove(x,y,(currStep&1)?零:交叉)
{

std::cout是一个循环还是一个单独的函数。@Plasmah是一个循环和一个单独的函数。输入确实需要分解成一个单独的函数。@JamesKanze:我没有使用xor:P@karlphillip这个例子是C。喜欢在C++中清理RAII。@ FIPPO如果是为了自我教育,那么做对它更重要。循环或分离。te函数。@Plasmah是一个循环和一个单独的函数。输入确实需要分解成一个单独的函数。@JamesKanze:我没有使用xor:P@karlphillip这个例子是C。喜欢在C++中清理RAII。@ FIPPO如果是为了自我教育,那么做它就更重要了。当然,这只是一个<代码> Goto 躲藏