Java:“打破”从被调用的方法循环? [英] Java: "Break" loop from called method?

查看:83
本文介绍了Java:“打破”从被调用的方法循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的小程序有点问题。
我有一个 JOptionPane 要求一个数字,如果这个数字小于10,那么一个循环只会持续不断地继续做其中的内容,保持在要求数字。在该循环中,我调用一个方法,使用int作为参数。在该方法中,我需要(不改变调用该方法的类中的任何代码)找出我输入的数字是否小于1.如果是,我需要调用另一个方法。那一点完成了。

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.

但是! mainloop不断滚动,因此它继续在循环中执行其他操作。我需要阻止它这样做,所以在方法的if语句中我需要打破方法所在的循环的特定迭代,并使其继续进行同一循环的新迭代,要求新号码。

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.

第一堂课(示例):

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
  }


推荐答案

你可以在这里做很多事情。最终你要做的事情应该取决于你的编码风格以及你想要完成的事情。

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.

选项1会有以下变化:

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

你可能会说,相当主观,视情况而定,这是不好的,因为终止条件的知识包含在循环中而不是执行实际工作的方法中。也许你的循环没问题。也许在其他情况下(或者可能与其他程序员一起?这非常属于品味问题。)你可能想让 myMethod 做出决定。一般来说,我自己的个人品味通常倾向于不在场景中的各种方法中分配场景知识,而是在一个地方。

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.

所以我写的大部分内容都是这里将是如何使 myMethod 决定是否终止。

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.

选项2 - myMethod 返回一个布尔值,表示我们应该终止:

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

但你可能会说 myMethod 已经想要返回其他类型。我来自非常C的背景,所以我最常用的成语是out参数。引导我选择3:

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:

选项3 - Out参数允许来电者决定终止:

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:

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

正如您所看到的,有很多选择。我相信人们可以花一整天时间谈论不同的方法。这是我见过的成语样本 - 我会警告你,自从我用Java做了很多事以来已经有一段时间了,所以我可能不会在这里编写最惯用的代码。 : - )

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. :-)

这篇关于Java:“打破”从被调用的方法循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆