0

在 arrayAverage 方法内部,avg 具有正确的值(我通过将 println (avg) 放在方法内部来测试它。当我从我的 main 方法调用该方法然后打印 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;
  }


}
4

1 回答 1

1

您的averageArray方法返回平均值,但您忘记将其分配给avgin main。目前没有任何内容为avg变量main提供值。改变

averageArray(count, arrayLength);

avg = averageArray(count, arrayLength);
于 2013-10-23T23:39:46.047 回答