windows 在 java 的 do while 循环中只执行一次指令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2840680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:27:55  来源:igfitidea点击:

execute an instruction only one time in a do while loop in java

javawindows

提问by Eddinho

in java, how can we execute an instruction only one time in a do while loop

在java中,我们如何在do while循环中只执行一次指令

do{
    int param;

    //execute this onty one time (depends of param)
    //other instructions instructions

}while(condition)

thank you

谢谢你

回答by JasCav

Putting the statement you want to execute only once is one way of doing it, but, of course, that assumes that the statement comes at the end or beginning of the loop, and doesn't depend on the conditions of what goes on in the loop (either before or after). If you have something like this:

将您想执行的语句只放入一次是一种方法,但是,当然,这假设语句出现在循环的末尾或开始处,并且不依赖于循环中发生的事情的条件循环(之前或之后)。如果你有这样的事情:

do {
  // do some stuff
  // one time condition
  // do some more stuff
} while(condition);

you won't easily be able to pull that information outside of the loop. If this is your problem, my suggestion would be to place some sort of condition around the one-time statement(s) and update the condition when the statement has run. Something like this:

您将无法轻松地将这些信息拉到循环之外。如果这是您的问题,我的建议是在一次性语句周围放置某种条件,并在语句运行时更新条件。像这样的东西:

boolean hasRun = false;
do {
  // do some stuff
  if(!hasRun) {
    // one time condition
    hasRun = true;
  }
  // do some more stuff
} while(condition);

回答by Bob Kaufman

How about:

怎么样:

// one-time-code here

do
{
}
while ( condition );

回答by Ignacio Vazquez-Abrams

Putting a statement outside of any loops will cause it to only be executed once every time the method is called.

将语句放在任何循环之外将导致它在每次调用该方法时只执行一次。

回答by Eddinho

Move the statement you want to execute only once outside the while loop.

将您只想执行一次的语句移到 while 循环之外。

回答by mikera

Assuming you want to keep the code inside the loop (other posters have suggested the obvious solution of moving the code outside!), you could consider using a flag to execute it only once:

假设您想将代码保留在循环内(其他海报提出了将代码移到外部的明显解决方案!),您可以考虑使用标志只执行一次:

boolean doneOnce=false;

do{

  if (!doneOnce) {

    \execute this only one time

    doneOnce=true;
  }

  \other instructions instructions

} while (condition)

Could be a useful construct if for example you had other instructions in the loop beforethe once-only code.

例如,如果您在循环中一次性代码之前有其他指令,则可能是一个有用的构造。

回答by janusz j

Add break:

添加中断:

do {
        int param;

        //execute this onty one time (depends of param)
        //other instructions instructions

        break;

    } while(condition);

回答by DSchlingel

Trying to answer exactly what you asked:

试图准确回答您的问题:

bool ranOnce = false;
do{
    int param;

    if (param == SOMECONDITION and !ranOnce){
      doWhatYouWant();
      ranOnce = true;
    }
    //other instructions instructions

}while(condition)

So there you have it, run something only once depending on param.

所以你有它,根据 param只运行一次。