gpt4 book ai didi

java - 变量可能尚未初始化

转载 作者:行者123 更新时间:2023-12-01 13:57:28 25 4
gpt4 key购买 nike

在 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;
}


}

最佳答案

您的averageArray方法返回平均值,但您忘记将其分配给main中的avg。当前没有任何东西可以为 main 中的 avg 变量提供值。改变

averageArray(count, arrayLength);

avg = averageArray(count, arrayLength);

关于java - 变量可能尚未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19554287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com