[英]Java Array Standard Deviation and Median
Need to add Median and Standard Deviation to the following program, but I have no idea where to start, I know what I want it to do but am not sure how to make it happen especially since the median has to come after the sorted numbers but if I put it in as void Median (), I loose the sorted array. 需要在以下程序中添加中位数和标准差,但是我不知道从哪里开始,我知道我想要做什么,但是不确定如何实现它,特别是因为中位数必须在已排序的数字之后,但是如果我将其放入void Median()中,则会松散排序后的数组。 Please help! 请帮忙! Any assistance is much appreciated: 非常感谢您的协助:
import java.util.Scanner;
import javax.swing.*;
public class JScannerO
{
public JScannerO(){}
public JScannerO(int A)
{
HowMuch = A;
}
void GenerateNumbers()
{
int k,Hide;
k = 0;
while ( k < HowMuch )
{
Hide = (int) (Math.random() * 100);
if ( Hide > 9 && Hide < 100 )
{
Hanson[k] = Hide;
k++;
}
}
}
void DisplayNumbers()
{
int k;
for ( k = 0; k < HowMuch; k++)
{
System.out.println("\t\tThe numbers are : " + Hanson[k]);
}
}
void BiggestNumber()
{
int k,Biggest;
Biggest = Hanson[0];
for ( k = 0; k < HowMuch; k++)
{
if ( Biggest < Hanson[k] ) Biggest = Hanson[k];
}
System.out.println("\n\n\t\tThe biggest number is : " + Biggest);
}
void SmallestNumber()
{
int k,Smallest;
Smallest = Hanson[0];
for ( k = 0; k < HowMuch; k++)
{
if ( Smallest > Hanson[k] ) Smallest = Hanson[k];
}
System.out.println("\n\n\t\tThe Smallest number is : " + Smallest);
}
void Average()
{
int k,Sum;
double Average;
Sum = 0;
for ( k = 0; k < HowMuch; k++)
{
Sum = Sum + Hanson[k];
}
Average = 1.0 * Sum / HowMuch;
System.out.format("\n\n\t\tThe Average number is : %7.2f",Average);
}
void Sort()
{
int k,Hide;
boolean DidISwap;
DidISwap = true;
while (DidISwap)
{
DidISwap = false;
for ( k = 0; k < HowMuch - 1; k++)
{
if ( Hanson[k] > Hanson[k+1] )
{
Hide = Hanson[k ];
Hanson[k] = Hanson[k+1];
Hanson[k+1] = Hide ;
DidISwap = true;
}
}
}
System.out.println("\n\n\n\t\tThe Sorted Numbers : \n");
}
private int HowMuch;
private int[] Hanson = new int[100];
}
This is run by using a calling program 这是通过使用调用程序运行
I don't understand your question. 我不明白你的问题。
You have a sorting algorithm there that does the work for you! 那里有一个排序算法可以为您完成工作!
Can't you just write a 你不能只写一个
public int median() {
Sort();
return Hanson[Math.floor(Hanson/2)];
}
? ?
This will cost you O(N) instead of O(1), but it is not error-prone and does what you need to do - especially since your "Hanson" array has only size 100 so we can just not care at this point. 这将使您花费O(N)而不是O(1),但是它不容易出错,并且可以满足您的需要-特别是因为“ Hanson”数组的大小只有100,所以我们现在不在乎。
As a final remark: function names are ALWAYS in lower camelCase, ie it's sort() and not Sort(), and it's calculateSomething() and not Calculate_Something() or any other variant. 最后一点:函数名称总是在较低的camelCase中,即它是sort()而不是Sort(),它是calculateSomething()而不是Calculate_Something()或任何其他变体。
There are kind of no exceptions to this rule, and that will make your code (much) more readable. 此规则没有例外,这会使您的代码(很多)更具可读性。 Consider installing CheckStyle in Eclipse (http://eclipse-cs.sourceforge.net/) in order to have an interactive guide to style when you are writing :-) 考虑在Eclipse(http://eclipse-cs.sourceforge.net/)中安装CheckStyle,以便在编写时具有交互式的样式指导:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.