绘制直方图Java(统计溢出)

时间:2011-08-06 18:45:45

标签: java

嗨所以我正在开发一个程序,它被设计成在一个数组中取一组随机高斯数字并将它们放入直方图的文本版本中(并不是很有趣)。我还有很长的路要走,但是我仍然坚持这一点,因为当我编译并运行我的代码时,我收到了java.lang.StackOverflowError的错误。它指的是我的客户端程序运行数字的统计数据(均值,求和,方差等),并说错误发生在第44行。这是我到目前为止的代码:

public class DescriptiveStatistics
{
public static void main(String args[])
{
 double data[] = DataSet.getGaussianNumbers(1000);


}
    public static double getMinimum()
    {
    double[] nums = DataSet.getGaussianNumbers(1000);
    double min = nums[0];
    System.out.println("min value is " + min);
    return getMinimum();
    }

    public static double getMaximum()
    {
    double[] nums = DataSet.getGaussianNumbers(1000);
    double max = nums[nums.length-1]; 
    return getMaximum();
    }


     public static double getSum()
     {
     double[] nums = DataSet.getGaussianNumbers(1000);



     return getSum();
     }

     public static double getMean()
     {
     int n1 = 1;
     int n2 = 3;
     int n3 = 5;
     int n4 = 7;
     int n5 = 9;
     int sum = n1 + n2 + n3 + n4 + n5;
     double mean = (double)sum/5.0;

     return getMean();
     }

     public static double getVariance()
     {
     int n1 = 1;
     int n2 = 3;
     int n3 = 5;
     int n4 = 7;
     int n5 = 9;
     int sum = n1 + n2 + n3 + n4 + n5;
     double mean = (double)sum/5.0;

     double difference1 = (n1 - mean) * (n1 - mean);
     double difference2 = (n2 - mean) * (n2 - mean);
     double difference3 = (n3 - mean) * (n3 - mean);
     double difference4 = (n4 - mean) * (n4 - mean);
     double difference5 = (n5 - mean) * (n5 - mean);
     double Sigma = difference1 + difference2 + difference3 + difference4 + difference5;  
     double variance = (0.2 * Sigma);

     return getVariance();

     }

     public static double getStandardDeviation()
     {
     int n1 = 1;
     int n2 = 3;
     int n3 = 5;
     int n4 = 7;
     int n5 = 9;
     int sum = n1 + n2 + n3 + n4 + n5;
     double mean = (double)sum/5.0;

     double difference1 = (n1 - mean) * (n1 - mean);
     double difference2 = (n2 - mean) * (n2 - mean);
     double difference3 = (n3 - mean) * (n3 - mean);
     double difference4 = (n4 - mean) * (n4 - mean);
     double difference5 = (n5 - mean) * (n5 - mean);
     double Sigma = difference1 + difference2 + difference3 + difference4 + difference5;  
     double variance = (0.2 * Sigma);

     double deviation = Math.sqrt(variance);

     return getStandardDeviation();
     }

}

我知道我的算术很乱,但它们有效。我只是不确定为什么我收到错误。如果还有其他明显的错误,请告诉我!谢谢!

3 个答案:

答案 0 :(得分:2)

 public static double getSum()
 {
    double[] nums = DataSet.getGaussianNumbers(1000);
    return getSum(); // ****
 }

哎哟!

你接近无限递归。此代码足以向您显示正在进行的操作:

public class TestGetSum {

   public static void main(String[] args) {
      double d = getSum();
   }

   public static double getSum() {
      double[] nums = getGaussianNumbers(1000);
      return getSum(); // ****
   }

   public static double[] getGaussianNumbers(int i) {
      return new double[i];
   }

}

因为getSum()在其return语句中调用自身,所以它会一直反复调用自身直到所有内存都用完为止。

答案 1 :(得分:2)

您的每一个方法都应该抛出StackOverflowError

public static double getMean()
 {
 int n1 = 1;
 int n2 = 3;
 int n3 = 5;
 int n4 = 7;
 int n5 = 9;
 int sum = n1 + n2 + n3 + n4 + n5;
 double mean = (double)sum/5.0;

 return getMean();
 }

通过在方法getMean()中返回getMean(),您将创建无限递归。我想你想要回归mean。 (所有其他方法都做同样的事情,所以你可能也想研究那些方法。)

答案 2 :(得分:0)

基本上,几乎所有的方法都是这样的:

while (true) {
    ...
}

这很糟糕,因为方法永远不会停止执行,除非程序崩溃并出现StackOverFlow错误。如果您已创建while (true)循环或任何其他无限循环,请确保循环内有breakreturn语句。如果您已创建方法,请确保未将返回值设置为方法名称,如以下示例所示:

boolean dontDoThis(int iWillCrash) {
    return dontDoThis();
}

希望这有帮助! :)

P.S。只是稍微减轻一点,here is a real StackOverFlow error!