具有整数

时间:2016-02-12 03:52:27

标签: java

我的程序需要帮助,它可以在文本文件中计算小于或等于1,000的整数的平均值,标准偏差和模式。然而,我的程序编译,我最终得到“null”作为标准差和平均值的输出,并且我的模式为“-1”。

 import java.util.Scanner;
 import java.io.File;
 import java.io.IOException; 
 public class Statistics
 {
  /**default constructor for Statistics class*/
  private static int [] unmodifiedNumbers, numbers;

   public Statistics()
  {
     unmodifiedNumbers = new int [1000];
  }

    /**reads numbers from files and puts them into an array*/
    public void readFromFile()
  {

      try
      {

        Scanner in = new Scanner(new File("numbers.txt"));
        int x;
        for(x = 0; in.hasNextInt(); x++)
            unmodifiedNumbers[x] = in.nextInt();
        numbers = new int [x];
        for(int y = 0; y < x; y++)
            numbers[y] = unmodifiedNumbers[y];
      }
      catch(Exception e)
      {
          System.out.println(e.getMessage());
     }
    }

    /**calculates and returns and prints average*/
    public static double avg()
    {
      double average = 0;
     try
    {
        Scanner in = new Scanner(new File("numbers.txt"));
        double total = 0;

        for(int x = 0; x < numbers.length; x++)  
            total += x;
        average = (double)total/numbers.length;
        System.out.println(average);

      }
      catch(Exception c)
      {
          System.out.println(c.getMessage());
      }
      return average;
    }

    /**calculates and displays mode*/
     public static void mode()
   { 

     int count = 0, mode = -1, maxCount = 0;
    try 
    {
       count = 0;
       Scanner in = new Scanner(new File("numbers.txt"));
      for(int x = 0; x < numbers.length; x++)
       {

        for(int y = 0; y < numbers.length; y++)
            if(numbers[y] == numbers[x])
                count++;
        if(count > maxCount)
        {
            mode = numbers[x];
            maxCount = count;
        }
     }
      }
      catch(Exception b)
    {
        System.out.println(b.getMessage());
    }
    System.out.println(mode);
  }

  /**calculates and displays standard deviation*/
   public static void stddev()
  {
  double stddev = 0;
  long total = 0;
  double average = avg();

    try
    {
        Scanner in = new Scanner(new File("numbers.txt"));
      for(int x = 0; x < numbers.length; x++)
        total += Math.pow((average - numbers[x]), 2);
      System.out.println(Math.sqrt((double)total/(numbers.length - 1)));
    }
   catch(Exception d)
   {
       System.out.println(d.getMessage());
   }
  System.out.println(stddev);
 }
 }

1 个答案:

答案 0 :(得分:0)

我对您的代码进行了一些更改以对其进行测试,并使其正常工作:

首先我添加main进行测试:

   public static void main(String args[]){
       readFromFile();
       avg();
       mode();
       stddev();
   }

您可以看到我拨打readFromFile

因此,我将readFromFile更改为静态。

/**reads numbers from files and puts them into an array*/
public static void readFromFile()

我改变了这个:

  /**default constructor for Statistics class*/
  private static int [] unmodifiedNumbers = new int [1000], numbers;

初始化未修改的数字

现在可行。