Java代码抛出错误,无法找到原因

时间:2017-03-22 14:52:14

标签: java

以下是我用于项目的代码。我正在设计一个概率模拟,允许用户输入结果数,命名结果,输入每个结果的概率,输入试验次数,然后运行模拟并打印结果。在解释Math.random()的结果时,我遇到了一些超出范围异常的问题。

public static void main(String [] args){

Scanner scan = new Scanner(System.in);

out("Welcome to Probability Simulator. How many outcomes would you like to generate?");
int numOfVars = scan.nextInt();
outln("");

String[] outcomes  = new String[numOfVars];
int[] outcomeCount = new int[numOfVars];
double[] probabilities = new double[numOfVars];

for (int i = 0; i < numOfVars; i++) {

  out("Enter outcome " + (i + 1) + "'s name: ");
  outcomes[i] = scan.next();
  outln("");

  probabilities[i] = -1;

}

for (int i = 0; i < numOfVars; i++) {

  while (!(probabilities[i] > 0 && probabilities[i] < 1)) {

    out("Enter probability for " + outcomes[i] + " (must be between 0 and 1, exclusive): ");
    probabilities[i] = scan.nextDouble();
    outln("");

  }

}

for (int i = 1; i < numOfVars; i++) {

  probabilities[i] += probabilities[i - 1];

}

int numOfResults = 0;

while (numOfResults < 2) {

  out("How many results would you like to generate? (must be more than one)");
  numOfResults = scan.nextInt();
  outln("");

}

int[] results = new int[numOfResults];
double[] random = new double[numOfResults];

for (int i = 0; i < numOfResults; i++) {

  random[i] = Math.random();

}

for (int i = 0; i < random.length; i++) {

  if (random[i] < probabilities[0])
    outcomeCount[0]++;
  else if (random[i] >= probabilities[probabilities.length - 2])
    outcomeCount[probabilities.length - 1]++;
  else {
    for (int n = 1; n < probabilities.length - 1; n++) {

      if (random[i] >= probabilities[n] && random[i] < probabilities[n + 1])
        outcomeCount[i]++;

    }

  }

}


for (int i = 0; i < probabilities.length; i++){

  System.out.println(probabilities[i]);

}

outln("");

for (int i = 0; i < numOfVars; i++){

  System.out.println(outcomeCount[i]);

}

outln("");

for (int i = 0; i < random.length; i++){

  System.out.println(random[i]);

}

scan.close();

}

public static void out(String text){

System.out.print(text);

}

public static void outln(String text){

System.out.println(text);

}

这是例外。

java.lang.ArrayIndexOutOfBoundsException: 4
at ProbabilitySimulation.main(ProbabilitySimulation.java:83)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:267)

此外,当使用2个以上的结果并且没有抛出错误时,outcomeCount数组并不能合计所需的结果数。

我无法弄清楚为什么会这样。我是一名java初学者,所以非常感谢有用的反馈,但请放轻松:)

(是的,我知道使用单独的方法作为System.out.print的快捷方式通常是不好的做法,但这是项目要求之一。)

1 个答案:

答案 0 :(得分:0)

想出来。将赋值更改为outcomeCount,如下所示。

for (int i = 0; i < random.length; i++) {

  if (random[i] / (probabilities[0] * 10) < 1)
    outcomeCount[0]++;

  else {

    for (int n = 1; n < numOfVars; n++) {

      if (random[i] >= (probabilities[n - 1] * 10) && random[i] < (probabilities[n] * 10))
            outcomeCount[n]++;

    }

  }

}

不再抛出错误,并将结果正确分配给相应的类别。感谢那些建议调试的人的帮助!