0

私はウェブサイトとプログラミング全般に不慣れなので、我慢してください。

私のプログラムは次の基準を満たしている必要があります。

  1. 正の整数nに続いてポイントの(x,y)座標を読み取り、これらの値を、行と2つの列nを持つpointsという名前の2次元配列に格納します。n

  2. distances [i] [j] = points[i]からpoints[j]までの距離で、distancesという名前のn行と列を持つ2次元配列を作成します。n

  3. averages [i] =距離nの行の平均で、averagesという名前の長さの1次元配列を作成します。i

  4. nどのポイントの平均距離が最小であるかを判別して印刷します。

パート#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;    
          }    
      }
4

1 に答える 1

0

これはそれを行う必要があります:

public static double average(double[] x)
{
   double avg = 0;
   for (double d: x)
     avg += d;
   avg /= x.length;
   return avg;
}
于 2013-03-01T09:31:04.027 に答える