C 程序中break语句的去除

C 程序中break语句的去除,c,while-loop,conditional,C,While Loop,Conditional,基本上,我的程序从一个随机数生成器中获取一个随机数,并计算通过该随机数并将其与另一个随机生成的数(内部循环)匹配所需的次数。然后,它获取计数并将其添加到总和中。然后外部循环运行50次。我的问题是我的教授不想在程序中使用goto或break语句。这是我的第一门计算机编程课程,我不太确定如何更改我的程序来删除break语句并保持它的正常工作。使用break语句,程序可以100%正常运行。提前感谢您的帮助 #include <stdio.h> #include <stdlib.h&g

基本上,我的程序从一个随机数生成器中获取一个随机数,并计算通过该随机数并将其与另一个随机生成的数(内部循环)匹配所需的次数。然后,它获取计数并将其添加到总和中。然后外部循环运行50次。我的问题是我的教授不想在程序中使用
goto
break
语句。这是我的第一门计算机编程课程,我不太确定如何更改我的程序来删除break语句并保持它的正常工作。使用
break
语句,程序可以100%正常运行。提前感谢您的帮助

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main()
{
    int randNumA = 0; int randNumB = 0; int i = 0.0;
    int iterationa = 0; int iterationb = 0;
    float avgIteration = 0.0;
    int sumIteration = 0;

    srand(time(NULL));

    for(i = 0; i < 50; i++)
    {
        int randNumB = rand() % 100;
        printf("The random number selected is %d\n", randNumB);
        int iterationb = 0;
        while (1)
        {
            int randNumA = rand() % 100;
            if (randNumA < randNumB)
            {
                iterationa++;
                iterationb++;
            }
            else if(randNumA > randNumB)
            {
                iterationa++;
                iterationb++;
            }
            else
            {
                iterationa++;
                iterationb++;
                printf("The final iteration was %d\n\n", iterationb);
                break;
            }
        }
    }
    sumIteration = sumIteration + iterationa;
    avgIteration = (float)sumIteration / (float)50;

    printf("The average amount of iterations for each attempt were %f\n", 
    avgIteration);
    return 0;
}
#包括
#包括
#包括
#包括
int main()
{
int randNumA=0;int randNumB=0;int i=0.0;
int迭代a=0;int迭代b=0;
浮动平均值=0.0;
int=0;
srand(时间(空));
对于(i=0;i<50;i++)
{
int randNumB=rand()%100;
printf(“选择的随机数为%d\n”,randNumB);
int迭代b=0;
而(1)
{
int randNumA=rand()%100;
if(randNumArandNumB)
{
迭代a++;
迭代b++;
}
其他的
{
迭代a++;
迭代b++;
printf(“最终迭代是%d\n\n”,迭代b);
打破
}
}
}
sumIteration=sumIteration+iterationa;
avgitation=(浮点)sum迭代/(浮点)50;
printf(“每次尝试的平均迭代次数为%f\n”,
avgitation);
返回0;
}

在顶部,您可以声明一个条件变量:
int isnumeither=0
,检查变量在while循环条件下是否保持false,然后在
else
块中将其设置为
1

下次循环检查条件时,由于条件的计算结果为false,循环将自动退出

例如:

while (isNumNeither == 0)
{
  int randNumA = rand() % 100;
  if (randNumA < randNumB)
  {
    iterationa++;
    iterationb++;
  }
  else if(randNumA > randNumB)
  {
    iterationa++;
    iterationb++;
  }
  else
  {
    iterationa++;
    iterationb++;
    printf("The final iteration was %d\n\n", iterationb);
    isNumNeither = 1;
  }
}
while(isnumeither==0)
{
int randNumA=rand()%100;
if(randNumArandNumB)
{
迭代a++;
迭代b++;
}
其他的
{
迭代a++;
迭代b++;
printf(“最终迭代是%d\n\n”,迭代b);
isNumNeither=1;
}
}

下面是关于您的问题的另一种看法,其中清除了各种错误。
主要变化:

  • 多次声明变量:即
    int randNumA
  • while循环是不必要的复杂。见评论
  • 简化的变量声明
  • 请参见代码中的注释:

    int main()
    {
        //It's great that you're initializing your variables.
        //You can do it as a comma separated list as such.
        int randNumA = -1,//so it definitely goes into the while loop the first time
            randNumB = 0, i = 0, iterationa = 0, iterationb = 0,  sumIteration = 0;
        float avgIteration = 0;
        srand(time(NULL));
    
        for(i=0; i < 50; i++)
        {
            randNumB = rand() % 100;//You've already declared, don't need int in front
            printf("The random number selected is %d\n", randNumB);
            iterationb = 0;
    
            while (randNumA != randNumB)//this will break the loop as is required
            {
                randNumA = rand() % 100;
                //next two lines occur in all three conditions, so bring them outside
                iterationa++;
                iterationb++;
                //No need for all three cases. Either equal or not!
                if (randNumA == randNumB)
                {
                    printf("The final iteration was %d\n\n", iterationb);
                }
            }
        }
    
        sumIteration += iterationa;//shorthand notation
        avgIteration = (float)sumIteration / 50;//casing one of these will suffice
    
        printf("The average amount of iterations for each attempt were %f\n", avgIteration);
        return 0;
    }
    
    intmain()
    {
    //您正在初始化变量,这很好。
    //您可以将其作为逗号分隔的列表来执行。
    int randNumA=-1,//所以它第一次肯定会进入while循环
    randNumB=0,i=0,iterationa=0,iterationb=0,sumIteration=0;
    浮点数=0;
    srand(时间(空));
    对于(i=0;i<50;i++)
    {
    randNumB=rand()%100;//您已经声明了,前面不需要int
    printf(“选择的随机数为%d\n”,randNumB);
    迭代b=0;
    while(randNumA!=randNumB)//这将根据需要中断循环
    {
    randNumA=rand()%100;
    //接下来的两行代码在所有三种情况下都会出现,所以请将它们带到外部
    迭代a++;
    迭代b++;
    //这三种情况都不需要,要么相等,要么不相等!
    if(randNumA==randNumB)
    {
    printf(“最终迭代是%d\n\n”,迭代b);
    }
    }
    }
    sumIteration+=iterationa;//速记法
    Avgitation=(float)sumIteration/50;//使用其中一个就足够了
    printf(“每次尝试的平均迭代次数为%f\n”,avgitation);
    返回0;
    }
    
    检查,它是否能满足您的要求。如果需要,我可以添加评论

    我不使用您的一些变量,因为它们在这里是不必要的,并且添加了两个新变量-
    cnt
    (while循环的迭代计数器),
    num\u iteration
    (迭代次数),而不是literal
    50
    数字

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main() {
        float sum_iteration = 0;
        int num_iteration = 50; 
        int cnt, i;
        int rand_num_a = 0;
        int rand_num_b = 0;
    
        srand(time(NULL));
    
        for(i = 0; i < num_iteration; i++) {
            rand_num_a = rand() % 100;
    
            printf("The random number selected is %d\n", rand_num_a);
            cnt = 0;
    
            do {
                rand_num_b = rand() % 100;
                cnt++;  
            }
            while(rand_num_a != rand_num_b); 
    
            printf("The final iteration was %d\n\n", cnt);
            sum_iteration += cnt;
        }   
    
        printf("The average amount of iterations for each attempt were %f\n", sum_iteration / num_iteration);
        return 0;
    }
    

    i=50应该这样做。它应该去哪里,@Nik?我错过了嵌套的
    ,而
    循环。那个建议行不通。你需要按照@ifconfig的建议去做,除了使用
    bool
    ,使用
    int
    ,因为
    C
    中没有
    bool
    谢谢,@Nik我忘了这一点。用于C++……:或者,如果在while循环上方声明
    randNumA
    ,也可以这样做:
    while(randNumA!=randNumB)
    。这可能是最简单的。你的意思是
    while(isnumeither==0)
    The random number selected is 39
    The final iteration was 71
    
    The random number selected is 55
    The final iteration was 119
    
    The random number selected is 25
    The final iteration was 10
    
    ... skip lines
    
    The random number selected is 81
    The final iteration was 132
    
    The random number selected is 37
    The final iteration was 300
    
    The random number selected is 22
    The final iteration was 19
    
    The average amount of iterations for each attempt were 89.580002