Java:“中断”从被调用的方法循环?
我的小程序有点问题。 我有一个 JOptionPane
请求一个数字,如果该数字小于 10,则一个循环将永远继续执行其中的操作,继续请求数字。在该循环内,我调用一个方法,并以 int 作为参数。在该方法中,我需要(无需更改调用该方法的类中的任何代码)找出我输入的数字是否小于 1。如果是,我需要调用另一个方法。那一点就完成了。
但!主循环不断滚动,因此它继续执行循环中的其他操作。我需要阻止它这样做,因此在方法的 if 语句中,我需要中断该方法所在循环的特定迭代,并使其继续到同一循环的新迭代,要求新号码。
第一类(示例):
number=Integer.parseInt( JOptionPane.showInputDialog( "bla bla" ) );
while (number !=- 10) {
themethod(number);
blah
blah
...
}
被调用的方法(示例):
public void themethod(int number) {
if (number<1) {
call the other method
break the iteration im in
}
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在这里做很多事情。最终你做什么应该取决于你的编码风格和你想要完成的任务。
选项 1 是以下内容的某种变体:
您可能会相当主观地并根据具体情况说,这很糟糕,因为终止条件的知识包含在循环中,而不是执行“实际工作”的方法中。也许对于你的循环来说没问题。也许在其他情况下(或者也许对于其他程序员?这很大程度上是一个品味问题。)您可能想让
myMethod
做出这个决定。一般来说,我个人的品味通常倾向于不将场景知识分布在源中的各种方法中,而是分布在一个地方。因此,我从这里开始写的大部分内容将是如何让
myMethod
做出是否终止的决定。选项 2 -
myMethod
返回一个布尔值,指示我们应该终止:但是您可能会说
myMethod
已经想要返回某种其他类型。我有很强的 C 背景,所以我最习惯的习惯用法是“out 参数”。让我看到选项 3:选项 3 - Out 参数让调用者决定终止:
或者也许您更喜欢例外:
选项 3:
正如您所看到的,有很多选项。我确信人们可以花一整天的时间来讨论不同的实现方法。这是我见过的习语示例 - 我会警告您,我已经有一段时间没有在 Java 中做过很多工作了,所以我可能不会在这里编写最惯用的代码。 :-)
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:
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: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:
Or maybe you're more a fan of exceptions:
Option 3:
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. :-)
添加一个返回值,指示 while 应该中断:
然后:
编辑:如果无法更改 while 代码,请从方法中抛出异常:
Add a return value indicating that the while should break:
Then:
Edit: If you can't change the while code, throw an exception from the method:
等等,我明白了吗?您有一个循环要求输入一个数字,如果该数字不是 -10,则执行某些操作,否则会中断?
如果是这样,请看一下:
否则,如果情况并非如此,并且您想在两种情况下中断,即 -10 情况和方法为 false 情况,您可以执行以下操作:
更改方法以返回布尔值。如果这是真的,那么它就不想打破。如果它是 false 那么它想要中断,那么执行以下操作:
如果您确实无法编辑循环,那么只需从方法中抛出异常即可!不过,这将退出您的整个程序。然而,我没有看到任何其他可能的方法来做到这一点。
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:
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:
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.
如果没有
themethod(int)
的返回值并且不更改其他类的代码,这是不可能的,因为实际上没有返回通信。你必须改变两者;如果不改变循环中的逻辑就无法完成它。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.