2016-02-12 69 views
0

我需要帮助我的程序,它需要从文本文件中计算小于或等于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); 
} 
} 

回答

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; 

要初始化unmodifiedNumbers

现在,它的工作原理。

相关问题