我正在研究一个统计计算器......仍然......我还有一些问题......我已经解决了大部分问题,但是,我仍然遇到问题..中位数排序但不是'实际上计算中位数是多少。另外,老实说我没有CLUE如何在java中做标准偏差。是的,我知道它是方差的sqrt,但我不知道如何计算方差或如何将数学方程式实际放入java中。我从朋友那里得到了很多帮助。我不幸遭受短期记忆丧失,我很容易忘记事情,没有任何借口,但我正在寻求一些帮助。 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
答案 0 :(得分:1)
虽然以下是未经测试的,但它们应该有效。对于中位数:
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