gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:38:53 25 4
gpt4 key购买 nike

我想知道,如果在这种情况下使用 goto ok 吗?你能建议更好的解决方案吗?我看到唯一一个在 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;
}
}
}

最佳答案

不要使用goto。使用 while (true) 循环,并在成功移动后break 退出。

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";
}

关于c++ - 我可以在这种情况下使用 goto 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9666548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com