更换C中的断路器

更换C中的断路器,c,do-while,break,C,Do While,Break,我正在写一个程序,这是一个电脑游戏的策划。当我第一次写它的时候,老师没有告诉我们不能用break,所以我用了。但不允许使用break。我正试图找到一种方法来取代中断,但改变逻辑对我来说很难理解。这是我的工作原始代码: //If number of matches is not equal to length, print the hints and check for pica's if(matches != LENGTH) { //First, print as

我正在写一个程序,这是一个电脑游戏的策划。当我第一次写它的时候,老师没有告诉我们不能用break,所以我用了。但不允许使用break。我正试图找到一种方法来取代中断,但改变逻辑对我来说很难理解。这是我的工作原始代码:

    //If number of matches is not equal to length, print the hints and check for pica's
    if(matches != LENGTH) {
        //First, print as many fermi hints as matches
        for(int i = 0; i < matches; i++) {
            printf("fermi ");
        }
        //Then, second loop to check for pica hints
        for(int i = 0; i < LENGTH; i++) {
            //We use the i index if not used before
            if(!usedNum[i]) {
                for(int j = 0; j < LENGTH; j++) {
                    //Check if the digit is the same for given indices if not used already on guess
                    if(!usedGuess[j] && num[i] == guess[j]) {
                        //If so, we have a match but on incorrect position
                        usedNum[i] = true;
                        usedGuess[j] = true;
                        used++;
                        printf("pica ");
                        break;
                    }
                }
            }
        }
        //If no used numbers, we print "bagels"
        if(used == 0) {
            printf("bagels");
        }
        printf("\n"); //line break after hints
    }
    //Finally, we return the num of matches
    return matches;
}
//如果匹配数不等于长度,则打印提示并检查pica的
如果(匹配!=长度){
//首先,打印尽可能多的费米提示
for(int i=0;i
我试图把它变成一个do-while循环

 if(matches != LENGTH) {
        //First, print as many fermi hints as matches
        for(int i = 0; i < matches; i++) {
            printf("fermi ");
        }
        //Then, second loop to check for pica hints
        for(int i = 0; i < LENGTH; i++) {
            //We use the i index if not used before
            if(!usedNum[i]) {
                do {
                    //Check if the digit is the same for given indices if not used already on guess
                        int j = 0;
                        //If so, we have a match but on incorrect position
                        usedNum[i] = true;
                        usedGuess[j] = true;
                        used++;
                        printf("pica ");                        
                    } while (usedGuess[j] || num[i] == guess[j]);
            } 
            }
        }
        //If no used numbers, we print "bagels"
        if(used == 0) {
            printf("bagels");
        }
        printf("\n"); //line break after hints
    }
    //Finally, we return the num of matches
    return matches;
}
if(匹配!=长度){
//首先,打印尽可能多的费米提示
for(int i=0;i
阅读植入术。我的原始代码可以声明为这样做,直到。然而,使用while循环的新代码是dothish,而该语句是true。所以对于原始代码,我有停止条件,对于我的新代码,我必须提出继续条件,对吗?是否有更好的逻辑循环可以使用


我看到另一篇帖子提到了子函数,但我们从未在课堂上讨论过它们,所以我认为我们不允许使用它们,尽管我仍然不确定如何实现。由于限制了
break的使用
似乎不合理,可能还有其他一些限制,我不知道每种方式是否都会被接受

更改
j
的值以退出循环:

for(int j = 0; j < LENGTH; j++) {
    if(!usedGuess[j] && num[i] == guess[j]) {
        /* snipped */
        j  = LENGTH;
    }
}
bool found = false;
for(int j = 0; !found && j < LENGTH; j++) {
    if(!usedGuess[j] && num[i] == guess[j]) {
        /* snipped */
        found = true;
    }
}

while循环有问题,在循环内将j初始化为0

for(int i = 0; i < LENGTH; i++) {
    if (!usedNum[i]) {
        int j=0, k=0;
        while (j < LENGTH && !k) {
           if(!usedGuess[j] && num[i] == guess[j]) {
                usedNum[i] = true;
                usedGuess[j] = true;
                used++;
                k++;
                printf("pica ");
            }
            j++;
        }
    }
}
for(int i=0;i

我认为应该这样做。

对于像您这样的情况,我会考虑将内部条件合并到循环条件中:

                int j;
                for (j = 0; j < LENGTH && (usedGuess[j] || num[i] != guess[j]); j++) /* empty */ ;
                if (j < LENGTH) {
                    // we have a match, but on an incorrect position
                    usedNum[i] = true;
                    usedGuess[j] = true;
                    used++;
                    printf("pica ");
                }
intj;
对于(j=0;j

循环只搜索满足所有所需条件的索引
j
。如果它终止时离开
j
小于
LENGTH
,则您知道
j
的最终值也满足所有其他条件。

似乎您的
中断可替换为
j=长度。具体的限制是什么?大多数要求与此无关。就像没有全局变量,只有最后一节课的材料,基本的东西。但是你的建议奏效了,我不知道为什么我没有想到。谢谢你一整天都在做这件事。谢谢,如果能用这些其他方法来做,那么休息的好处是什么?或者这些是唯一的选择,因为这种独特的情况。
break可以立即从循环中跳出。这里的第一个和第二个选项是可用的,因为在这之后没有代码可以执行以产生可见的效果
goto
也可以立即跳出循环,但可能更难理解其内涵。
break
清晰简洁地表达了意图。它通常比依赖于使循环的终止条件得到满足的选项更容易使用,而且它比这些技术更能抵抗代码更改。
goto
方法也有这些属性,尽管它可能不像
break
那样清晰
                int j;
                for (j = 0; j < LENGTH && (usedGuess[j] || num[i] != guess[j]); j++) /* empty */ ;
                if (j < LENGTH) {
                    // we have a match, but on an incorrect position
                    usedNum[i] = true;
                    usedGuess[j] = true;
                    used++;
                    printf("pica ");
                }