【问题标题】:checking a condition only in the first cycle and in the rest cycles execute some code仅在第一个周期中检查条件,在其余周期中执行一些代码
【发布时间】:2012-12-12 03:36:16
【问题描述】:

有没有办法只在 for 循环的第一个循环中检查条件,如果条件被评估为真,则在其余循环中执行一些代码?我有一个 for 循环和两个基本条件,只需要在第一个循环中检查。如果其中任何一个为真,则在所有其他周期中正在执行一段或另一段代码。我无法在其他周期中检查这些条件,但第一个,因为两者都是正确的,这不是我需要的((

    public void someMethod() {
    int index;
    for (int j = 0; j < 10; j++) {
        if (j == 0 && cond1 == true && cond2 == true) {
            methodXForTheFirstCycle();// this method will change cond2
            methodXForTheRestCycles();// not the right place to put it
        } else if (j == 0 && cond1 == true) {// and cond2 == false
            methodYForTheFirstCycle();// this method will change cond2
            methodYForTheRestCycles();// not the right place to put it
        }
    }
}

【问题讨论】:

  • 每个if-else if 块都有一个else 块。好好利用它。
  • 如果它只是第一次运行,并且它总是第一次运行,请检查 for 循环的条件 outside,将该条件的值设置为变量,然后使用该变量来控制稍后执行的代码。另外,你的问题有点做作 - 显示实际代码并解释你实际在做什么会更容易,这样我们就可以看到你的逻辑和更真实的建议。

标签: java for-loop conditional-statements


【解决方案1】:

如果我正确理解您想要什么,您可以做的是检查这两个条件一次,如果它们失败则中断。如果成功,则将标志设置为 true,以便在后续迭代中绕过检查。

【讨论】:

    【解决方案2】:

    我有点困惑,因为如果 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();   
          }
        }
      }
    }
    

    【讨论】:

    • cond1 == true 是多余的事实,en.wikipedia.org/wiki/Redundancy_theory_of_truth(又名措辞;)
    • 正要发布答案,然后意识到它与您的答案太接近而无法打扰。所以我提交了一个小的编辑,将方法调用移动到我认为你想要的位置。如果我错了,请随意拒绝 =)
    • 谢谢!这似乎是我一直在寻找的东西!)如果它不起作用,我会发布实际的代码(已经输入了它,但是这里已经很晚了,所以我会尝试利用你的建议明天早上就对了!)@MrSmith42
    【解决方案3】:

    尝试使用一个新的标志(布尔值),也没有必要将布尔值比较为真,如下所示:

    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();
        }
     }
    }
    

    【讨论】:

      【解决方案4】:

      我建议你稍微松开循环。

      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;
         }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多