0

arrayAverage メソッド内で、avg は正しい値を持っています (メソッド内に println (avg) を配置してテストしました。メイン メソッドからメソッドを呼び出してから 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に割り当てるのを忘れていましたmainavg現在、変数に値を与えるものはありませんmain。変化する

averageArray(count, arrayLength);

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