我在编程方面相对较新,并且很难弄清楚如何循环遍历数组直到计数器找到零,并且当它找到零一次时,执行一个操作并退出循环。这是我到目前为止的循环:
for (int i = 0; i<13; i++)
{
if(pHand[i] == 0)
{
pHand[i] = deal(numArray);
printf("%d\n", i);
printHand(pHand, "Your");
}
}
目前,它循环遍历数组,直到找到零,调用 deal(),打印 pHand 的值,然后循环返回相同的序列,直到 i=0。请帮忙。我完全不知道如何解决这个问题。
最佳答案
break 语句可用于退出封闭循环(例如 while、do-while 或 for)或 switch。
for (int i = 0; i<13; i++)
{
if(pHand[i] == 0)
{
pHand[i] = deal(numArray);
printf("%d\n", i);
printHand(pHand, "Your");
break;
}
}
// code will continue executing here if the for loop condition becomes
// false (i is 13) or if the break statement is reached.
关于c - 循环数组,找到零,执行操作,停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39542623/