【问题标题】:continuing next iteration of **for** loop in java if any method fails in another class?如果任何方法在另一个类中失败,则继续在 java 中进行 **for** 循环的下一次迭代?
【发布时间】:2015-06-19 12:01:27
【问题描述】:

有没有办法从java中的另一个类更新for循环中的循环变量?

例如,让我们考虑一个类 mytest.java,如下所示:

 public class mytest()
        {
                public static void main(String[] args)
                {
                   for(int i=0;i<10;i++)
                   {
                       new checker().check(i);
                   }
                }
        }

现在考虑类 checker.java 如下:

public class checker()
{
    public boolean check(int i)
    {
         if(i==5)
         {
             //Here if value of i is 5, i don't want to do any 
             //more operation but just update the for loop of
             // of mytest class and continue with later iterations.
         }
         else
         {
              //there may be many more operations like calling other
              // methods or other class.
         }
   }
}

【问题讨论】:

  • 不,但您可以使用一个字段,该字段可从当前方法之外访问。
  • 所以你的意思是这不可能?
  • 你班上的if (i != 5) { ... }有什么问题checker(顺便说一句:请把它命名为Checker)?如果循环变量为 5,则检查器不执行任何操作并返回。然后循环变量会自动递增。

标签: java class for-loop


【解决方案1】:

您可以从 check() 方法中返回一个整数值并将其分配给 for 循环变量 i。

根据你的例子:

public class checker
{
    public int check(int i)
    {
         if(i==5)
         {
             //Here if value of i is 5, i don't want to do any 
             //more operation but just update the for loop of
             // of mytest class and continue with later iterations.
         }
         else
         {
              //there may be many more operations like calling other
              // methods or other class.
         }
       return i;

   }
}

并且在您的 mytest 类中,您可以将值分配给 for 循环变量。

public class mytest
{
      public static void main(String[] args)
      {
            for(int i=0;i<10;i++)
            {
                int temp = new checker().check(i);
                if (condition)
                  i= temp;
             }
        }
 }

【讨论】:

  • 我的主要问题是直接更新循环变量,如果任何方法在另一个类中失败,则继续后面的迭代。以上不是具体案例。希望你能理解。
  • 您可以在类检查器 for 循环中使用全局静态变量。并通过访问静态值来设置它。
  • 对不起,这不符合我的目的!
【解决方案2】:

你不能从另一个类分配循环变量,我很高兴你不能这样做! 现在这并不完全符合您的要求,也不是最漂亮的代码,但它可以完成这项工作:

公共类 myTest {

private int loopVariable;


public static void main(String[] args)
{
    myTest myTest = new myTest();
    myTest.mainLoopd(myTest);
}

public void mainLoopd(myTest myTest){
    checker checker = new checker(myTest);

    for(loopVariable=0;loopVariable<10;loopVariable++)
    {
        checker.check();
        System.out.println(loopVariable);
    }
}

public int getLoopVariable(){
    return this.loopVariable;
}

public void setLoopVariable(int loopVariable){
    this.loopVariable = loopVariable;
}

}

检查器类将如下所示:

公共类检查器 {

private myTest myTest;

public checker(myTest myTest){
    this.myTest = myTest;
}

public void check()
{
    if(this.myTest.getLoopVariable()==2)
    {
        this.myTest.setLoopVariable(5);
    }
    else
    {
        //there may be many more operations like calling other
        // methods or other class.
    }
}

}

希望对你有帮助

【讨论】:

  • 我认为你没有理解我的问题的本质。我不仅想更新循环变量,而且只要 if 语句在检查器类中返回 true,就应该执行下一次迭代。您的代码所做的是设置循环变量,但继续其他部分。记住 check 方法可以在 if 语句之后有很多操作,如果 if 语句不为真则应该执行。
猜你喜欢
  • 2017-01-15
  • 2010-12-23
  • 1970-01-01
  • 2019-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多