0
我对网站和编程一般都很陌生,所以请耐心等待。Java中一维数组的平均长度
我的程序必须满足以下条件:
读取一个正整数
n
,其次是(x,y
)坐标的n
点,在二维阵列中存储这些值命名的点与n
行和两列。用
n
行和n
列创建一个二维数组,距离[i] [j] =从点[i]到点[j]的距离。创建名为平均长度为
n
的一维数组,平均值[i] =距离的行的平均值为i
。确定并打印哪个点的平均距离最小。
部分我似乎被难倒在部分#3,我将如何计算行的平均距离?
这是我的代码到目前为止,如果任何人都可以帮助我完成它,我将不胜感激。同时指出我所拥有的任何错误。
import java.util.Scanner;
public class Java
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("How many points: ");
int n = input.nextInt();
double[][] points = new double[n][2];
for(int i = 0; i < n; i++)
{
//prompt or and get coordinates
//points[i][0] = input.nextDouble();
//points[i][1] = input.nextDouble();
}
double[][] distances = new double[n][n];
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
distances[i][j] = distance(points[i], points[j]);
double[] averages = new double[n];
for(int i = 0; i < n; i++)
averages[i] = average(distances[i]);
int which_one = minimum_location(averages);
System.out.printf("Point # %d has a smallest average of %f\n", which_one,
averages[which_one]);
}
public static double distance
(double x1, double y1, double x2, double y2)
{
return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
{
public static double average(double[] x);
//problem area
}
{
public static int minimum_location(double[] x);
if (shortestDistance > distance);
{
p1 = i;
p2 = j;
shortestDistance = distance;
}
}
#3是要求平均[I]是距离[I] [0] ..距离[I] [N]的平均值。这与要求1d阵列的平均值相同,因为第一项对于您计算的每个平均值是不变的。 – Thomas 2013-03-01 05:59:57