62,625
社区成员
发帖
与我相关
我的任务
分享
package org.cm.test;
import java.util.Random;
public class Location {
private int x;
private int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
static int[][] locations;
public Location locationNearest(Location MyLocation){
locations = new int[5][2];
Random rd = new Random();
double[] mm = new double[5];
for (int i = 0; i < locations.length; i++) {
System.out.println("-------"+i+"-------");
for (int j = 0; j < locations[i].length; j++) {
locations[i][j]=rd.nextInt(100);
System.out.print(locations[i][j]+" ");
}
mm[i] = Math.pow(Math.pow((MyLocation.x+0.0-locations[i][0]), 2)+Math.pow((0.0+MyLocation.y-locations[i][1]), 2), 1.0/2);
System.out.println(mm[i]);
}
int minIndex = 0;
double min = mm[0];
for (int i = 0; i < mm.length; i++) {
if(min>mm[i]){
min = mm[i];
minIndex = i;
}
}
MyLocation.setX(locations[minIndex][0]);
MyLocation.setY(locations[minIndex][1]);
return MyLocation;
}
public static void main(String[] args) {
System.out.println(Math.pow(1.414, 2));
Location location = new Location();
location.setX(0);
location.setY(0);
Location locationNearest = location.locationNearest(location);
System.out.println("最近的店铺坐标为:x="+locationNearest.getX()+",y="+locationNearest.getY());
}
}
