2013-10-23 163 views
0

在arrayAverage方法中,avg具有正確的值(我通過在方法內部放置println(avg)來測試它。當我從主方法調用方法並打印平均值時,netbeans告訴我,該變量可能尚未初始化。我必須做一些錯誤,但我想不出什麼。變量可能未初始化

/*****************************************************************************\ 
* randomIntCount                | 
* program generates a list of random numbers from 1-10, calculates    | 
* the frequency of each number (1-10), calculates the mean, and    | 
* outputs mean and frequency to a txt file          |        | 
* created for CSci 111               | 
* Last modified 10/23/2013              | 
*****************************************************************************/ 

package randomintcount; 

/** 
*@author Steve Pesce 
* 
*/ 
public class RandomIntCount { 

    /** 
    * Main method generates an array of 1000 random numbers and calls other 
    * methods to calculate frequency, calculate average and output these to a 
    * text file 
    */ 
    public static void main(String[] args) { 

     int [] randomNumbers = new int [1000];    //1000 random numbers  

     int i;             //loop counter 

     int [] count = new int [11];  //frequency of 1-10 in randomNumbers[] 

     double avg;  /*        Ʃ[ number * frequency] 
          calculation of mean(avg): ---------------------- 
                  arrayLength 
     */ 

     int arrayLength;        //used in mean calculation 


     count[0] = 0; 

     for (i = 0; i < randomNumbers.length; i++) //fill randomNumbers with 
     {           //random numbers 
      randomNumbers[i] = (int) ((Math.random() * 10) + 1); 
     } 

     arrayLength = randomNumbers.length; 
                 //------------------------ 
     for (i = 1; i < 11; i++)      //count controlled loop  
     {           //that uses frequencyCounter 
     frequencyCounter(randomNumbers, count, i); //method for each possible 
     }           //value      
               //----------------------------- 
     for (i = 1; i < count.length; i++)  
     { 
      System.out.println (count [i]);  //TEST DELETE ME! 
     } 

     System.out.println("length " + arrayLength); //TEST DELETE ME! 

     averageArray(count, arrayLength); 

     System.out.println (avg); 
    } 
/******************************************************************************\ 
| frequencyCounter counts the frequency of a number in a given array   | 
| uses numbers[], count[], and i            | 
| numbers[]: the sample array of numbers          | 
| count []: array that holds the frequency of a value in numbers[n]  | 
|   i: used as a counter for count []         | 
|                    | 
| returns: count                | 
\----------------------------------------------------------------------------**/  
    public static void frequencyCounter (int[] numbers, int[] count, int i) 
    { 

     int n;         //for count controlled loop  

     for (n = 0; n <1000; n++)   //for loop that checks if loop 
     {         // counter from method calling loop(i) 
      if (numbers[n] == i)   //is equal to values from numbers[] 
        count[i]++;   //and incriments count sub counter 
     } 






    } 
/******************************************************************************\ 
| this method finds the average of an array using        | 
|              Ʃ[ number * frequency]| 
|        calculation of mean(avg): ---------------------| 
|               arrayLength  | 
|method paramaters: int[] count            | 
|     int arrayLength           | 
| Method returns mean as (double avg)           | 
|_____________________________________________________________________________*/ 
    public static double averageArray (int[] count,int arrayLength) 
    { 

     double totalSum = 0; 

     double avg = 0; 


     int i;           //for count controlled loop 

     for (i = 1; i < count.length; i++)//uses formula calculation of mean 
     {        //to find avg. CTRL+F calculation of mean  
      totalSum = ((count[i] * i) +totalSum);//to see formula used (document- 
     }              //ed in main method) 

     avg = (totalSum/ arrayLength); 

     System.out.println(avg); //just a test to see if avg was initialized 

     return avg; 
    } 


} 

回答

1

averageArray方法返回的平均水平,但你忘了給它main分配給avg。目前沒有任何數值爲mainavg變量值。更改

averageArray(count, arrayLength); 

avg = averageArray(count, arrayLength);