gpt4 book ai didi

java - 统计计算器 - 中位数问题和标准差 java

转载 作者:行者123 更新时间:2023-12-01 12:14:47 24 4
gpt4 key购买 nike

我正在开发一个统计计算器...仍然...我仍然遇到一些问题..我已经解决了大部分问题,但是,我仍然遇到问题..中位数排序但没有'实际上计算中位数是多少。另外,老实说我不知道​​如何在 java 中进行标准偏差。是的,我知道它是方差的平方根,但我不知道如何计算方差或如何实际将数学方程放入java中。在这方面我得到了一位 friend 的很多帮助。不幸的是,我患有短期内存丧失,我很容易忘记事情,仍然没有借口,但我正在寻求一些帮助。 Java 确实让我大吃一惊,我知道这是很容易的事情,而且会变得更难,但我正在努力学习。

    import java.util.Scanner; // Scanner is in the java.util package

public class BasicStatisticsCalculator
{
static final int NUMBER_OF_ELEMENTS=6;
static int[] numbers=new int[NUMBER_OF_ELEMENTS];



public static void main(String[] args)
{

do
{
switch(menu())
{
case 'a': insertSeries(numbers); break;
case 'b': mean(numbers); break;
case 'c': median(numbers); break;
case 'd': mode(numbers); break;
case 'e': sd(numbers); break;

case 'f': showSeries(numbers); break;
case 'g': greaterThanMean(numbers); break;
case 'h': smallerThanMean(numbers); break;
case 'i': bubbleSort(numbers); break;
case 'j': fullReport(numbers); break;

case 'q': System.exit(0);
}
}
while(true);
}

//Menu Display
static char menu(){
Scanner input=new Scanner(System.in);

System.out.println("\n\nBASIC STATISTICS CALCULATOR");
System.out.println(" a. Insert Series");
System.out.println(" b. Mean");
System.out.println(" c. Median");
System.out.println(" d. Mode");
System.out.println(" e. Standard Deviation");

System.out.println(" f. Show Series");
System.out.println(" g. Show Elements greater than the mean");
System.out.println(" h. Show Elements smaller than the mean");
System.out.println(" i. Show series in order");
System.out.println(" j. Show Full Report");

System.out.println("\n q. Quit");
System.out.print("\nInsert option: ");

return(input.next().charAt(0));
}//END menu display

/*Insert series */
static void insertSeries(int[] numbers)
{
java.util.Scanner input = new java.util.Scanner(System.in);
for (int i = 0; i < numbers.length; i++)
{
System.out.print("Enter a new number: ");
numbers[i] = input.nextInt();
}
}//END USER INPUT PROMPT


/*Show series */
static void showSeries(int[] numbers)
{
System.out.print("\nSeries: ");
for (int i = 0; i < numbers.length; i++)
{
System.out.print(numbers[i]+" ");
}
}//END DISPLAY SERIES

/*Show series overloaded with two parameters */
static void showSeries(int[] numbers, String s)
{
System.out.print("\n"+s);
for (int i = 0; i < numbers.length; i++)
{
System.out.print(numbers[i]+" ");
}
}


/*Find the mean value */
static void mean(int[] numbers)
{
float sum=0;
for (int i = 0; i < numbers.length; i++) sum+=numbers[i];
System.out.print("\nMean: "+sum/numbers.length);
}//END MEAN

static float mean2(int[] numbers)
{
float sum=0;
for (int i = 0; i < numbers.length; i++) sum+=numbers[i];
return (sum/numbers.length);
}

/*Find the median value */
public static double median(int[] numbers)
{
int[] numbers2=bubbleSort(numbers);
for (int i = 0; i < numbers.length; i++);

int indexB = numbers.length / 2;
int indexA = (numbers.length - 1) / 2;
double median;
if (numbers.length % 2 == 0)


median = ((double)(numbers[numbers.length/2] + (double)numbers[numbers.length/2 - 1]))/2;
else
median = ((double) (numbers2[indexA] + numbers2[indexB])) / 2;

System.out.println("\nThe median number is : " + median);
return 0;

}//END MEDIAN



/*Find the mode value */
static void mode(int[] numbers)
{
//Create a frequency vector
int offset=min(numbers);
int[] numbers2=numbers.clone();
for(int i=0; i<numbers2.length; i++) numbers2[i]=numbers[i]-offset;

int interval=max(numbers)-offset;
int[] frequency=new int[interval+1];
for(int i=0; i<numbers.length; i++) frequency[numbers2[i]]++;

if (maxIndex(frequency)==-1) System.out.print("\nMode: All elements have the same frequency");
else System.out.print("\nMode: "+((maxIndex(frequency))+offset));
}//End Mode


/*Find the maximum value */
static int max(int[] numbers)
{
int max=numbers[0];
for (int i = 1; i < numbers.length; i++) if(max<numbers[i]) max=numbers[i];
return max;
}//End Max Value

/*Find the minimum value */
static int min(int[] numbers)
{
int min=numbers[0];
for (int i = 1; i < numbers.length; i++) if(min>numbers[i]) min=numbers[i];
return min;
}

/*Find the maximum value */
static int maxIndex(int[] frequency)
{
boolean noMax=true;
//Check to see if there is no maximum
int max=max(frequency);
for (int i = 0; i < frequency.length; i++) if (max!=frequency[i] && frequency[i]!=0) noMax=false;

//Return the index associated to the maximum
if(noMax) return -1;
else
{
max=frequency[0];
int index=0;
for (int i = 0; i < frequency.length; i++) if(max<frequency[i]) {max=frequency[i]; index=i;}
return index;
}//End else
}//End MAX Value index


/*Find the standard deviation */
/*Find the standard deviation */
static void sd(int[] numbers){


//Standard deviation
double x = 0;
double average = mean2(numbers);

//Subtract mean from each number and square it
for (int i = 0; i < numbers.length; i++) x = x +((numbers[i] - average)*(numbers[i] - average));

//find mean
x = x / (numbers.length-1);

//square root
double sd = Math.sqrt(x);

//Show Standard deviation
System.out.print("\nStandard deviation: "+ sd);

}

/*Show values greater than mean */
static void greaterThanMean(int[] numbers)
{
float sum=0;
float mean;
for (int i = 0; i < numbers.length; i++) sum+=numbers[i];
mean=sum/numbers.length;

System.out.print("\nElements greater than mean: ");
for (int i = 0; i < numbers.length; i++) if (numbers[i]>mean) System.out.print(numbers[i]+" ");
}

/*Show values smaller than mean */
static void smallerThanMean(int[] numbers)
{
float sum=0;
float mean;
for (int i = 0; i < numbers.length; i++) sum+=numbers[i];
mean=sum/numbers.length;

System.out.print("\nElements smaller than mean: ");
for (int i = 0; i < numbers.length; i++) if (numbers[i]<mean) System.out.print(numbers[i]+" ");
}


/*Bubble sort algorithm */
public static int[] bubbleSort(int[] series)
{
if (series!=null)
{
int[] vector=series.clone();

int aux;
for (int i=0; i<vector.length; i++)
{
for(int j=0; j<vector.length-(i+1); j++)
{
if(vector[j]>vector[j+1])
{
aux=vector[j+1];
vector[j+1]=vector[j];
vector[j]=aux;
}
}
}
showSeries(vector,"Ordered series: ");
}
return numbers;
}//END SORT


/*Show all statistics*/
static void fullReport(int[] numbers)
{
showSeries(numbers);
mean(numbers);
median(numbers);
mode(numbers);
sd(numbers);
greaterThanMean(numbers);
smallerThanMean(numbers);
bubbleSort(numbers);
}

}//End BasicStatisticCalculator

最佳答案

虽然以下内容未经测试,但它们应该可以工作。对于中位数:

public static double median(int[] numbers) {
Arrays.sort(numbers); // sort numbers
int len = numbers.length;

if (len % 2 == 0) // length is even
return (numbers[len/2] + numbers[len/2-1]) / 2.0d;

else // length is odd
return (double) numbers[len/2];

}

标准差是数字与平均值的平均距离,因此首先计算数字集的平均值(您已经有一个函数)。然后做这样的事情:

static double sd(int[] numbers) {

double mean = mean(numbers);
double std_dev = 0;

for (int i=0; i<numbers.length; i++) {
std_dev += Math.pow(mean - (double) numbers[i], 2);
}

// This gets the population standard deviation
return Math.sqrt(std_dev / (double) (numbers.length-1) );


}

有关总体标准差的更多信息,请参见此处:Population Standard Deviation

关于java - 统计计算器 - 中位数问题和标准差 java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27084131/

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