Java 仅在第一个周期和剩余周期中检查条件,执行一些代码
有没有办法只在for循环的第一个循环中检查一个条件,如果条件的计算结果为true,则在其余循环中执行一些代码?我有一个for循环和其中的两个基本条件,只需要在第一个循环中进行检查。如果其中一个为真,则在所有其他循环中执行一段或另一段代码。除了第一个循环,我无法在其他循环中检查这些条件,因为这两个循环都是正确的,并且这不是我需要的((Java 仅在第一个周期和剩余周期中检查条件,执行一些代码,java,for-loop,conditional-statements,Java,For Loop,Conditional Statements,有没有办法只在for循环的第一个循环中检查一个条件,如果条件的计算结果为true,则在其余循环中执行一些代码?我有一个for循环和其中的两个基本条件,只需要在第一个循环中进行检查。如果其中一个为真,则在所有其他循环中执行一段或另一段代码。除了第一个循环,我无法在其他循环中检查这些条件,因为这两个循环都是正确的,并且这不是我需要的(( public void someMethod(){ 整数指数; 对于(int j=0;j
public void someMethod(){
整数指数;
对于(int j=0;j<10;j++){
如果(j==0&&cond1==true&&cond2==true){
methodXForTheFirstCycle();//此方法将更改cond2
methodXForTheRestCycles();//不是放置它的正确位置
}如果(j==0&&cond1==true){//和cond2==false
methodYForTheFirstCycle();//此方法将更改cond2
methodYForTheRestCycles();//不是放置它的正确位置
}
}
}
如果我正确理解您的要求,您可以做的是检查这两个条件一次,如果失败则中断。如果成功,则将标志设置为true,以便在后续迭代中绕过检查。我有点困惑,因为如果j!=0
,您的代码将不起任何作用-那么为什么要执行循环呢
我会将循环拆分。拉出调用第一个方法的I==0
,然后是I=1..9
我认为你的描述就是这个意思:
public void someMethod() {
int index;
if (cond1) {
if (cond2) {
methodXForTheFirstCycle();
for (int j = 1; j < 10; j++) {
methodXForTheRestCycles();
}
} else {
methodYForTheFirstCycle();
for (int j = 1; j < 10; j++) {
methodYForTheRestCycles();
}
}
}
}
public void someMethod(){
整数指数;
如果(条件1){
如果(条件2){
methodXForTheFirstCycle();
对于(int j=1;j<10;j++){
方法xfortherestcycles();
}
}否则{
methodYForTheFirstCycle();
对于(int j=1;j<10;j++){
methodYForTheRestCycles();
}
}
}
}
尝试使用一个新的标志(布尔值),也不需要将布尔值比较为true,如下所示:
public void someMethod() {
int index;
boolean firstConditionSuccess = false;
for (int j = 0; j < 10; j++) {
if (j == 0 && cond1 && cond2) {
methodXForTheFirstCycle();
firstConditionSuccess = true;
} else if (j == 0 && cond1) {// and cond2 == false
methodYForTheFirstCycle();
firstConditionSuccess = true;
}
if(firstConditionSuccess ){
methodYForTheRestCycles();
}
}
}
public void someMethod(){
整数指数;
布尔值firstConditionSuccess=false;
对于(int j=0;j<10;j++){
如果(j==0&&cond1&&cond2){
methodXForTheFirstCycle();
成功=正确;
}如果(j==0&&cond1){//和cond2==false,则为else
methodYForTheFirstCycle();
成功=正确;
}
如果(成功){
methodYForTheRestCycles();
}
}
}
我建议你稍微解开你的环
if (cond1)
// j == 0
if (cond2)
methodXForTheFirstCycle();
else
methodYForTheFirstCycle();
cond2 = !cond2;
for (int j = 1; j < 10; j++) {
if (cond2)
methodXForTheRestCycle();
else
methodYForTheRestCycle();
cond2 = !cond2;
}
}
if(cond1)
//j==0
如果(条件2)
methodXForTheFirstCycle();
其他的
methodYForTheFirstCycle();
cond2=!cond2;
对于(int j=1;j<10;j++){
如果(条件2)
methodXForTheRestCycle();
其他的
methodYForTheRestCycle();
cond2=!cond2;
}
}
对于每个if else if
块,您都有一个else
块。利用它。如果它只是第一次运行,并且总是第一次运行,请检查for循环外部的条件,将该条件的值设置为一个变量,然后使用该变量控制以后执行的代码。此外,您还需要这个问题有点做作——展示实际代码和解释实际操作会更容易,这样我们就可以看到您的逻辑和真正的建议。cond1==true是一个多余的真理,(又称verbiage;)我打算发布一个答案,然后意识到它离你的答案太近了,所以我提交了一个小的编辑,它将方法调用移动了一点,使之成为我认为你想要的。如果我错了,请随意拒绝它=)谢谢!这似乎是我一直在寻找的!)如果它不起作用,我将发布实际的代码(已经打好了,但现在已经很晚了,所以明天早上我会尽量利用你的建议!)@Smith42
if (cond1)
// j == 0
if (cond2)
methodXForTheFirstCycle();
else
methodYForTheFirstCycle();
cond2 = !cond2;
for (int j = 1; j < 10; j++) {
if (cond2)
methodXForTheRestCycle();
else
methodYForTheRestCycle();
cond2 = !cond2;
}
}