Java 用户是否可以在给定的时间内重复外部方法调用

Java 用户是否可以在给定的时间内重复外部方法调用,java,methods,Java,Methods,我想这是一个简单的问题,但我在Google或Stack Overflow上都找不到答案。我是Java新手,希望你能帮助我。如果我在编写问题和代码时使用了错误的术语,我提前表示歉意 我的问题是,是否有任何方法可以编写重复外部方法调用的代码? 我调用的方法是shootwealth,它存在于名为wealth的类中。我从另一个名为shootwithwearm的方法调用该方法,该方法存在于名为Soldier的类中。我的职业Soldier在ArrayList中有一个名为weaponList的武器清单。我想编

我想这是一个简单的问题,但我在Google或Stack Overflow上都找不到答案。我是Java新手,希望你能帮助我。如果我在编写问题和代码时使用了错误的术语,我提前表示歉意

我的问题是,是否有任何方法可以编写重复外部方法调用的代码?

我调用的方法是
shootwealth
,它存在于名为
wealth
的类中。我从另一个名为
shootwithwearm
的方法调用该方法,该方法存在于名为
Soldier
的类中。我的职业
Soldier
ArrayList
中有一个名为
weaponList
的武器清单。我想编写一个代码,重复用户给定的
shootWithWearm
参数
usedAmmo
的次数。我当前的代码正在工作,但它只是用1减少弹药(因为它是在
射击武器
方法中指定的)

这是我在“士兵”课上的方法。

public void shootWithWeapon(String weaponChoice,int usedAmmo)

int index = 0;
while(index < weaponList.size()){
Weapon listWeapon = weaponList.get(index);
String usedWeapon = listWeapon.getName(); //returns weapon name
int xBullets = weaponList.getBulletAmount(); //returns amount of bullets in object

if(weaponChoice.equals(usedWeapon) && usedAmmo < xBullets) {
listWeapon.shootWeapon(); //This is the method I want to repeat * usedAmmo
System.out.println("Your weapon have been used. You have " + weaponList.getBulletAmount() + " bullets left".); }

index++;
}
}
public void shootWeapon()
{
bulletAmount=bulletAmount - 1;
}

嗯,你总是可以在循环中调用一个方法,例如,
for(inta=usedAmmo;a>0;a--){listBlarm.shootBlarm();}
也许你需要一个?@Thomas他不能直接用数量作为他的shootMethod的参数吗?@Abra你为什么认为他需要一个递归方法?@JawadElFou是的,这可能是有道理的,但这取决于该方法的作用:考虑检查命中率等-这将相当于一个循环,因此这取决于OP,问题是关于多次调用一个方法(我不会在这里为设计而烦恼,这将导致一个非常广泛的讨论):)