Java: “Break” loop from called method?

笑着哭i 提交于 2019-12-01 03:31:46

问题


I've got a bit of an issue with my little program. I have a JOptionPane asking for a number, and if that number is less than 10, a loop that just continues on and on forever doing what's in it, keeping on asking for numbers. Inside that loop, I call a method, with an int as parameter. In the method, I need to (without altering any of the code in the class that calls the method) find out whether the number I entered is less than 1. If it is, I need to call on another method. That bit's done.

But! The mainloop keeps rolling, so it keeps doing the other stuff in the loop. I need to stop it from doing that, so in the if-statement in the method I need to break that specific iteration of the loop the method is in, and make it go on to a new iteration of the same loop, asking for a new number.

The first class (example):

number=Integer.parseInt( JOptionPane.showInputDialog( "bla bla" ) );
while (number !=- 10) {
  themethod(number);
  blah
  blah
  ...
}

The called method (example):

public void themethod(int number) {
  if (number<1) {
    call the other method
    break the iteration im in
  }

回答1:


There are a number of things you can do here. Ultimately what you do should depend on your coding style and what you are trying to accomplish.

Option 1 would be some variation of:

for (;;)
{
   int number = /* ... */;
   myMethod(number);
   if (number == -10)
      break;
}

You might say, rather subjectively and depending on circumstances, that this is bad, because knowledge of the termination condition is contained in the loop rather than the method doing the "real work". Maybe for your loop that's OK. Maybe in other circumstances (or perhaps with other programmers? This is very much a matter of taste.) you might want to make myMethod make that decision. In general my own personal taste usually leans towards not having scenario knowledge be distributed throughout various methods in source, but in one place.

So most of what I'll write from here on will be how to make myMethod make the decision about whether or not to terminate.

Option 2 - myMethod returns a boolean indicating we should terminate:

for (;;)
{
   int number = /* ... */;
   if (myMethod(number))
      break;
}

boolean myMethod(int number)
{
   // TODO - do stuff

   return number == -10;
}

But you might say that myMethod already wants to return some other type. I come from very much a C background so the idiom I'm most used to would be the "out parameter". Leading me to option 3:

Option 3 - Out parameter lets caller decide to terminate:

public class CancelIndicator
{
   public boolean shouldCancel;
};

CancelIndicator cancel = new CancelIndicator();

while (!cancel.shouldCancel)
{
   int number = /* ... */;
   myMethod(number, cancel);
}

int myMethod(int number, CancelIndicator cancel)
{
   // TODO - do stuff.

   cancel.shouldCancel = (number == -10);

   return /* ... */;
}

Or maybe you're more a fan of exceptions:

Option 3:

public class CancellationException extends Exception
{
}

try
{
   for (;;)
   {
      int number = /* ... */;
      myMethod(numberl);
   }
}
catch (CancellationException ex)
{
}

void myMethod(int number) throws CancellationException
{
   // TODO - do stuff.

   if (number == -10)
      throw new CancellationException();
}

As you can see there are a number of options. I'm sure one could spend a whole day talking about different ways to do it. Here is my sample of idioms I have seen - I'll warn you that it's been some time since I've done much in Java so I might not write the most idiomatic code here. :-)




回答2:


Add a return value indicating that the while should break:

public boolean themethod(int number) {
   if(number<1) {
     call the other method
     return true;
   }
   return false;
}

Then:

while(number !=-10) {
  if(themethod(number)) break;
  bla
  bla
  ...
}

Edit: If you can't change the while code, throw an exception from the method:

public void themethod(int number) {
   if(number<1) {
     call the other method
     throw new RuntimeException("Negative Number");
   }
}



回答3:


Wait, so did I get this straight? You have a loop that asks for a number, does something if the number is not -10, and otherwise breaks?

If so, look at this:

while(true) {
    number=Integer.parseInt( JOptionPane.showInputDialog( "bla bla" ) );
    if(number == -10) {
        break;
    }
    themethod(number);
}

Otherwise, if this is not the case, and you want to break in two cases, the -10 case and the method is false case, you can do this:

Change your method to return a boolean. If it's true, then it doesn't want to break. If it's false then it wants to break, then do this:

while(true) {
    number=Integer.parseInt( JOptionPane.showInputDialog( "bla bla" ) );
    if(number == -10) {
        break;
    }
    if(!themethod(number)) break;
}

If you really can't edit the loop, then just throw an exception from the method! That will exit your entire program, though. I don't see any other possible way of doing this, however.




回答4:


Without a return value for themethod(int) and without changing the other class's code, this isn't possible since as it is, there is no return communication. You'll have to change both; it can't be done without changing the logic in the loop.



来源:https://stackoverflow.com/questions/8466010/java-break-loop-from-called-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!