刷新 Java 参数
我正在为一个名为 robocode 的编程游戏编写程序。问题就在这里:
void wallScan(boolean While){
stop();
getStraight();
turnGunRight(90);
if(startabsolute){
straight=true;
}
while (While){
ahead(10000000);
turnRight(90);
}
resume();
}
您可能不理解大部分代码,因为它扩展了 robocode.Robot,但我的问题出在变量 While
中。循环不会结束,因为该方法获取参数一次并且它是 true,因此该方法成为一个永恒循环,但是有没有办法刷新方法参数,因为我不想每次调用它时都进行 while 循环方法?
I am writing a program for a programming game called robocode. The problem is here:
void wallScan(boolean While){
stop();
getStraight();
turnGunRight(90);
if(startabsolute){
straight=true;
}
while (While){
ahead(10000000);
turnRight(90);
}
resume();
}
You might not understand most of the code as it extends robocode.Robot, but my problem is in the variable While
. The loop doesn't end as the method gets the argument once and it is true so the method becomes an eternal loop but is there a way to refresh the method argument as I don't want to make a while loop every time I call this method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应该用大写字母编写参数。所以应该是 while 而不是 While。然而这是不允许的,因为 while 是一个关键字。因此,首先更改方法中传递的参数。
那么你的问题是,你用参数调用该方法。由于它是您传递的原始布尔值,因此在执行 wallScan 方法期间无法从其他方法、调用、类等更改该值,因此 while 循环永远不会结束。
相反,您应该在包含此方法的类中创建一个成员字段,并为其提供有意义的方式。在示例中我只是将其称为 whileCondition。
因此,您可以设置导致 while 循环从方法外部终止的条件。
You shouldn't write you parameters in capital letters. So it would be while instead of While. However this isn't allowed because while is a keyword. So first change your argument passed in the method.
Then your problem is, that you call the method with the argument. Since it is a primitive boolean value you pass, the value can't be changed from another method, call, class, etc. during the execution of your wallScan method and therefore the while loop never finishes.
Instead you should for example create a member field in the class containing this method an give it a meaningful way. in the example i just call it whileCondition.
So you can set the condition which leads to the termination of the while loop from outside your method.
在我看来,您不需要一个布尔值 - 您想要的东西每次您请求一个布尔值时都会返回一个布尔值。举一个简单的例子:(
可怕的名字,但希望你能想出更好的东西。)然后你可以这样写:
另一种形式是通用接口,例如
Provider
one来自 Guice:您的方法可能需要一个
Provider出于同样的目的。
就我个人而言,我更喜欢这种方法而不是 Sebi 的方法 - 它允许你的类代表棋盘本身的状态(或其他) - 一个特定的机器人是否应该停止并不感觉它应该是同一状态的一部分。据我所知,它实际上是该方法的本地方法。
It seems to me that you don't want a single boolean value - you want something which will return you a boolean every time you ask for one. As a simple example:
(Horrible names, but hopefully you can come up with something better.) You can then write:
An alternative form of this would be a generic interface, such as
Provider<T>
one from Guice:Your method could take a
Provider<Boolean>
for the same purpose.Personally I prefer this approach over that of Sebi - it allows your class to represent the state of the board itself (or whatever) - whether one particular robot should stop doesn't feel like it should be part of the same state. It's effectively local to this method, as far as I can see.