2014-11-01 134 views
0

你好,我遇到了這個錯誤,而試圖運行這個程序,我想不出有什麼地方錯了:在錯誤過濾程序:異常線程「main」 java.lang.ArrayIndexOutOfBoundsException:0

異常線程 「主要」 java.lang.ArrayIndexOutOfBoundsException:0 在PercolationStats.main

這裏是代碼:

/**************************************************************** 
    * 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); 
    } 
} 
+0

當你調用你的主要方法時,你傳遞參數嗎? – user2336315 2014-11-01 14:53:12

回答

2

這看起來像來自其他來源的一些相當仔細的代碼。

在哪裏幾乎可以肯定是失敗的是它期望你提供一些參數程序。您可以在代碼頂部的註釋中看到如何執行此操作。試着用

java PercolationStats 20 1000 

看看會發生什麼。

如果你在eclipse中運行它,你可以在運行配置中設置參數。

+0

Tnx 4你的幫助,我明白了;) – 2014-11-01 15:05:54

相關問題