在java中,在do while循环中只执行一次指令
在java中,如何在do while循环中只执行一次指令在java中,在do while循环中只执行一次指令,java,windows,Java,Windows,在java中,如何在do while循环中只执行一次指令 do{ int param; //execute this onty one time (depends of param) //other instructions instructions }while(condition) 谢谢你怎么样: // one-time-code here do { } while ( condition ); 那么: // one-time-code here do {
do{
int param;
//execute this onty one time (depends of param)
//other instructions instructions
}while(condition)
谢谢你怎么样:
// one-time-code here
do
{
}
while ( condition );
那么:
// one-time-code here
do
{
}
while ( condition );
将语句置于任何循环之外会导致每次调用该方法时只执行一次。将语句置于任何循环之外会导致每次调用该方法时只执行一次。将要执行的语句移至while循环之外只执行一次。移动所需语句只在while循环外执行一次。将要执行的语句只执行一次是一种方法,但是,当然,这假设语句位于循环的末尾或开头,并且不取决于循环中发生的情况(无论是之前还是之后)。如果你有这样的东西:
do {
// do some stuff
// one time condition
// do some more stuff
} while(condition);
boolean hasRun = false;
do {
// do some stuff
if(!hasRun) {
// one time condition
hasRun = true;
}
// do some more stuff
} while(condition);
你不可能轻易地把这些信息从循环中拉出来。如果这是您的问题,我的建议是在一次性语句周围放置某种条件,并在语句运行时更新条件。大概是这样的:
do {
// do some stuff
// one time condition
// do some more stuff
} while(condition);
boolean hasRun = false;
do {
// do some stuff
if(!hasRun) {
// one time condition
hasRun = true;
}
// do some more stuff
} while(condition);
将您想要执行的语句只执行一次是一种方法,但是,当然,这假设语句出现在循环的末尾或开头,而不取决于循环中发生的情况(之前或之后)。如果你有这样的东西:
do {
// do some stuff
// one time condition
// do some more stuff
} while(condition);
boolean hasRun = false;
do {
// do some stuff
if(!hasRun) {
// one time condition
hasRun = true;
}
// do some more stuff
} while(condition);
你不可能轻易地把这些信息从循环中拉出来。如果这是您的问题,我的建议是在一次性语句周围放置某种条件,并在语句运行时更新条件。大概是这样的:
do {
// do some stuff
// one time condition
// do some more stuff
} while(condition);
boolean hasRun = false;
do {
// do some stuff
if(!hasRun) {
// one time condition
hasRun = true;
}
// do some more stuff
} while(condition);
假设你想把代码保存在循环中(其他的海报已经提出了在外部移动代码的明显解决方案),你可以考虑使用一个标志来执行一次:
boolean doneOnce=false;
do{
if (!doneOnce) {
\\execute this only one time
doneOnce=true;
}
\\other instructions instructions
} while (condition)
可能是一个有用的构造,例如,在一次代码之前,在循环中有其他指令。假设要将代码保持在循环内(其他的海报已经提出了在外部移动代码的明显解决方案!),可以考虑使用标志只执行一次:
boolean doneOnce=false;
do{
if (!doneOnce) {
\\execute this only one time
doneOnce=true;
}
\\other instructions instructions
} while (condition)
如果循环中的其他指令位于一次性代码之前,则可能是一个有用的构造。添加中断:
do {
int param;
//execute this onty one time (depends of param)
//other instructions instructions
break;
} while(condition);
添加中断:
do {
int param;
//execute this onty one time (depends of param)
//other instructions instructions
break;
} while(condition);
试图准确回答您的问题:
bool ranOnce = false;
do{
int param;
if (param == SOMECONDITION and !ranOnce){
doWhatYouWant();
ranOnce = true;
}
//other instructions instructions
}while(condition)
这样,您就可以根据参数只运行一次了。尝试准确回答您的问题:
bool ranOnce = false;
do{
int param;
if (param == SOMECONDITION and !ranOnce){
doWhatYouWant();
ranOnce = true;
}
//other instructions instructions
}while(condition)
因此,您可以根据参数只运行一次。不,我可以,因为我在while循环之前有一个for循环,所以for循环中存在相同的问题,并且指令依赖于参数。while循环中存在一个参数。请显示一个更大的代码示例,其中包括所述for循环。这将使我们能够提供更具体的答案。:)不幸的是,我认为这是一个过于简单化的解决方案(尽管,他的问题没有说清楚)。看看我的答案。@Jason:同意,但是根据所问的,这是最简单的答案。根据OP的评论,我希望我们能很快对这个问题有一个更清晰的了解。不,我可以,因为我在while循环之前有一个for循环,所以for循环中也有同样的问题,指令依赖于一个参数。while循环中存在一个参数。请展示一个更大的代码示例,其中包括said for循环。这将使我们能够提供更具体的答案。:)不幸的是,我认为这是一个过于简单化的解决方案(尽管,他的问题没有说清楚)。看看我的答案。@Jason:同意,但是根据所问的,这是最简单的答案。根据OP的评论,我希望我们能很快对这个问题有一个更清晰的了解。我想到了这一点,但我不知道它是否存在另一种方式。我想到了这一点,但我不知道它是否存在另一种方式