variable may not have been initialized

落花浮王杯 提交于 2019-12-20 06:18:16

问题


inside of the arrayAverage method, avg has the right value (I tested it by puting println (avg) inside of the method. When i call the method from my main method and then print avg, netbeans tells me that the variable may not have been initialized. I must be doing something wrong, but I cannot figure out what.

/*****************************************************************************\
*  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:


Your averageArray method returns the average, but you forgot to assign it to avg in main. Nothing currently gives the avg variable in main a value. Change

averageArray(count, arrayLength);

to

avg = averageArray(count, arrayLength);


来源:https://stackoverflow.com/questions/19554287/variable-may-not-have-been-initialized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!