![](/img/trans.png)
[英]trying to call method I'm getting java.lang.IndexOutOfBoundsException
[英]Java: I keep getting an error when trying to call the shooterExperiments method. I'm not sure what I'm doing wrong or exactly how to call a method
我遇到的問題是最后一行。 我在此部分收到一個錯誤,提示“ 找不到符號 ”。 這不是我的整個程序,只是第一部分。
public class Chap6Project
{
public double shooterExperiment(int dualsPerExperiment, String[] shooters, double[] accuracies, ProbabilitySupplier p)
{
shooters[0] = "aaron";
shooters[1] = "bob";
shooters[2] = "charlie";
String aaron = shooters[0];
String bob = shooters[1];
String charlie = shooters[2];
accuracies[0] = 0.33;
accuracies[1] = 0.5;
accuracies[2] = 1.0;
int duelCount = 0;
boolean aaronAlive = true;
boolean bobAlive = true;
boolean charlieAlive = true;
Chap6Project project = new Chap6Project();
ProbabilitySupplier random = new ProbabilitySupplier();
double rateOfSuccess = project.shooterExperiment(1000, shooters, accuracies, random);
while (duelCount < 1000)
{
int aaronKills = 0;
int aaronWin = 0;
double aaronAccuracy = random.getAsDouble();
if (aaronAccuracy == 0.33)
{
if (charlieAlive != true)
{
aaron.shooterExperiment(1000, bob, accuracies[1], random);
您可以這樣聲明aaron變量:
String aaron = shooters[0];
然后嘗試像這樣使用它:
aaron.shooterExperiment(1000, bob, accuracies[1], random);
並在此方法中完成所有操作:
public double shooterExperiment(int dualsPerExperiment, String[] shooters,
double[] accuracies, ProbabilitySupplier p)
aaron變量是一個String,並且Strings沒有shooterExperiment(...)
方法。 也許您打算在其他變量上調用此方法? 鑒於您發布的內容,很難說。 似乎您正在嘗試從自身內部調用shooterExperiment
方法,這意味着進行遞歸調用,並使用錯誤的方法參數和錯誤的對象(String對象)執行此操作,而這些都不有意義。
您將要澄清您的問題以及您的代碼應該做什么。
其他事宜:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.