简体   繁体   English

如何在Java中组织3个嵌套循环?

[英]How to organize 3 nested loops in java?

Here is my loops structure. 这是我的循环结构。

int k = 0;
do {
    ....                        

    do {
        fc = Recognize();
        ....
        do {
            ....
        } while(fc != false);

        k++;
    } while(k != 20);

    if (k == 20) {
        return;
    }
} while(true);

I need to end all loops when either k equals 20 or fc equals true. k等于20或fc等于true时,我需要结束所有循环。
I tried different ways to do this but failed. 我尝试了不同的方法来执行此操作,但失败了。 The code above is my last attempt. 上面的代码是我最后的尝试。 Need help in fixing that. 需要帮助解决该问题。

   grabber.start();
   int k=0;
   do {   
     grabber.stop();
     grabber.start();

     img = grabber.grab();
     if (img != null) {
       canvas.showImage(img);
       canvas.pack();
       cvWaitKey(0);
     }
     do {  
       fc=  Recognize();
       grabber.start();
       do {
         img = grabber.grab();
         if (img != null) {
            CvHaarClassifierCascade cascade = new 
            CvHaarClassifierCascade(cvLoad(XML_FILE));
            CvMemStorage storage = CvMemStorage.create();
            CvSeq sign = cvHaarDetectObjects(img,
              cascade,storage, 1.5, 3,CV_HAAR_DO_CANNY_PRUNING);

            cvClearMemStorage(storage);
            total_Faces = sign.total();     

            for(int i = 0; i < total_Faces; i++){
              CvRect r = new CvRect(cvGetSeqElem(sign, i));
              cvRectangle (img,
                cvPoint(r.x(),r.y()),
                cvPoint(r.width()+r.x(),r.height()+ r.y()),
                CvScalar.RED,2,CV_AA,0);

                x=r.x();
                y=r.y();
                h=r.height();
                w=r.width();
              }         

              cvFlip(img, img, 1);
              canvas.showImage(img);
              canvas.pack();
              cvWaitKey(0);
           }
         } while(fc!=false);

         k++;    
       }  while(k!=21);  
     } while(true);  
  }

Short answer: don't do that. 简短的答案:不要那样做。

Check out Clean code by Robert Martin; 查看Robert Martin的Clean代码; and understand & apply the single layer of abstraction principle. 并理解和应用抽象原理的单层

There are good reasons why various tools measure "code complexity" by especially looking for such kind of nested structures. 有充分的理由说明各种工具通过特别寻找这种嵌套结构来衡量“代码复杂性”。

I understand that many people with a more "mathematical" background think that it is OK when computer programs resemble "mathematical" structures. 我知道许多具有“数学”背景的人都认为,当计算机程序类似于“数学”结构时,这是可以的。 But well: that is wrong. 但是好吧:那是错误的。 Computer programs are written ... to be read by humans. 编写计算机程序...供人类阅读。 When you read a piece of source code, the main thing that counts is: how long does it take you to understand what is going on. 当您阅读一段源代码时,最重要的是:您需要多长时间才能了解正在发生的事情。

I guarantee you, no matter how you structure a loop in a loop in a loop; 我保证,无论您如何在一个循环中构造一个循环。 in one week from now you will have major difficulties to understand what is going on. 从现在开始的一周内,您将很难理解正在发生的事情。 Therefore it is worth to sit down for an hour or more to come up with a better solution. 因此,有必要坐一个小时或更长时间来提出更好的解决方案。 Unfortunately, your example doesn't show the real problem you are trying to solve; 不幸的是,您的示例并没有显示您要解决的真正问题。 thus it is a hard to give concrete tips on how to do that in your case. 因此,很难就您的情况给出具体的提示。

while(k!=21 && fc){...} is enough. while(k!=21 && fc){...}就足够了。 Or do {...} while(k!=21 && fc); 或者do {...} while(k!=21 && fc); if need be. 如果需要的话。 The outermost loop is useless as well as some of the if s in the middle. 最外面的循环以及中间的一些if都没有用。

The only loop that needs to be written is 唯一需要编写的循环是

int k = 0;
do {
      fc = Recognize();
      k++  ;
   } while(k!=20 || fc );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM