[英]error in percolation program: Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0
Hello I encountered this error while trying to run this program and I cannot figure out what's wrong with it: 您好,我在尝试运行此程序时遇到此错误,但我无法弄清楚它出了什么问题:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at PercolationStats.main
线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:在PercolationStats.main处为0
and here is the code: 这是代码:
/****************************************************************
* Compilation: javac PercolationStats.java
* Execution: java PercolationStats N T
* Dependencies: Percolation.java, StdStats.java, StdRandom.java,
* StdOut.java
*
* % java PercolationStats 20 1000
* mean = 0.591100
* stddev = 0.046068
* 95% confidence interval = 0.582071, 0.600129
*
* % java PercolationStats 200 100
* mean = 0.593257
* stddev = 0.016242
* 95% confidence interval = 0.592251, 0.594264
*
***************************************************************/
public class PercolationStats {
private int experimentsCount;
private Percolation pr;
private double[] fractions;
public PercolationStats(int N, int T) {
if (N <= 0 || T <= 0) {
throw new IllegalArgumentException("Given N <= 0 || T <= 0");
}
experimentsCount = T;
fractions = new double[experimentsCount];
for (int expNum = 0; expNum < experimentsCount; expNum++) {
pr = new Percolation(N);
int openedSites = 0;
while (!pr.percolates()) {
int i = StdRandom.uniform(1, N + 1);
int j = StdRandom.uniform(1, N + 1);
if (!pr.isOpen(i, j)) {
pr.open(i, j);
openedSites++;
}
}
double fraction = (double) openedSites / (N * N);
fractions[expNum] = fraction;
}
}
public double mean() {
return StdStats.mean(fractions);
}
public double stddev() {
return StdStats.stddev(fractions);
}
public double confidenceLo() {
return mean() - ((1.96 * stddev()) / Math.sqrt(experimentsCount));
}
public double confidenceHi() {
return mean() + ((1.96 * stddev()) / Math.sqrt(experimentsCount));
}
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
int T = Integer.parseInt(args[1]);
PercolationStats ps = new PercolationStats(N, T);
String confidence = ps.confidenceLo() + ", " + ps.confidenceHi();
StdOut.println("mean = " + ps.mean());
StdOut.println("stddev = " + ps.stddev());
StdOut.println("95% confidence interval = " + confidence);
}
}
This looks like some fairly carefully written code from another source. 这看起来好像是来自其他来源的一些经过精心编写的代码。
Where it is almost certainly failing is that it is expecting you to provide some arguments to the program. 几乎可以肯定的失败之处在于,它期望您为程序提供一些参数 。 You can see how to do this in the comments at the top of the code.
您可以在代码顶部的注释中查看如何执行此操作。 Try running it with
尝试使用
java PercolationStats 20 1000
and see what happens. 看看会发生什么。
If you're running this from inside eclipse, you can set the arguments in the run configuration. 如果从eclipse内部运行,则可以在运行配置中设置参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.