2016-02-12 68 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

現在,它的工作原理。

相關問題