摆脱程序中的break语句

Posted

技术标签:

【中文标题】摆脱程序中的break语句【英文标题】:Getting rid of break statement in program 【发布时间】:2018-04-10 06:14:21 【问题描述】:

基本上,我的程序从随机数生成器中获取一个随机数,并计算通过该确切数字与另一个随机生成的数字(内部循环)匹配所需的次数。然后它进行计数并将其添加到总和中。然后外循环运行 50 次。我的问题是我的教授不想在程序中使用gotobreak 语句。这是我的第一门计算机编程课程,我不太确定如何更改程序以删除 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;

【问题讨论】:

i = 50; 应该这样做。 它应该去哪里,@Nik? 我错过了嵌套的while 循环。这个建议行不通。你需要按照@ifconfig 的建议去做,除了bool 之外,使用int,因为C 中没有bool 这样的东西 谢谢,@Nik 我忘了。习惯了 C++.... :) 或者,如果您在 while 循环上方声明 randNumA,您也可以这样做:while (randNumA != randNumB)。这可能是最简单的。 【参考方案1】:

这是对您的问题的另一种看法,清理了各种错误。 主要变化:

    多次声明变量:即int randNumA while 循环不必要地复杂。参见 cmets。 简化的变量声明。

查看代码中的 cmets:

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;

【讨论】:

【参考方案2】:

检查,它是否按照您的意愿行事。如果需要,我可以添加 cmets。

我不使用您的一些变量,因为它们在这里是不必要的,而且还添加了几个新变量 - cnt(while 循环的迭代计数器),num_iteration(迭代次数),而不是文字 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;

输出

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

【讨论】:

【参考方案3】:

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

下次循环检查条件时,循环将自动退出,因为条件评估为假。

例如:

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 (isNumNeither == 0)

以上是关于摆脱程序中的break语句的主要内容,如果未能解决你的问题,请参考以下文章

While循环中的break语句出现问题

C++ 流程控制⁽¹⁰⁾|break 语句

c++中的break,continue,goto语句

SWITCH语句中如果执行了一条CASE语句是空的后如果没有BREAK会不会执行DEFAULT语句(其他CASE也没BREAK)

★循环中的continue和break语句,写结果题,第5题

switch语句的用法?