import java.util.Scanner;
public class WorldDistance {
public enum Planet {
MERCURY (2.4397e6),
VENUS (6.0518e6),
EARTH (6.37814e6),
MARS (3.3972e6),
JUPITER (7.1492e7),
SATURN (6.0268e7),
URANUS (2.5559e7),
NEPTUNE (2.4746e7);
public final double radius; //in metres
Planet(double radius) {
this.radius = radius;
}
public String toString() {
String original = name();
return original.charAt(0) + original.substring(1).toLowerCase();
}
}
private static boolean inRange(int value, int low, int high) {
return value > low && value < high;
}
public static Planet selectPlanet(Scanner in) {
Planet[] options = Planet.values();
int selected;
do {
System.out.println("On which planet?");
for (int i = 0; i < options.length; i++) {
System.out.println((i + 1) + ". " + options[i]);
}
System.out.print("Enter choice: ");
selected = in.nextInt();
if (! inRange(selected, 1, options.length)) {
System.err.println("Invalid selection\n");
}
} while (! inRange(selected, 1, options.length));
return options[selected];
}
public static double worldDistance(double lat1, double long1, double lat2,
double long2, final double radius)
{
double a;
a = Math.pow(Math.sin(Math.toRadians(lat2 - lat1)/2), 2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.pow(Math.sin(Math.toRadians(long2 - long1)/2), 2);
return Math.round(2*radius * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))/1000);
}
public static double euclideanDistance(double x1, double y1,
double x2, double y2)
{
return Math.hypot(x1 - x2, y1 - y2);
}
public static String formatTime(double hours) {
long h = Math.round(Math.floor(hours));
return String.format("%d hours %d minutes", h, Math.round(60*(hours - h)));
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String origin, destination;
double lat1, long1, lat2, long2;
Planet where;
double distance;
double speed;
System.out.println("Travel Distance & Time Calculator");
System.out.println();
System.out.print("Enter origin: ");
origin = in.next();
System.out.print("Enter destination: ");
destination = in.next();
System.out.println("Enter their respective latitude and longitude coordinates " +
"as decimal values,\nseparated by spaces (e.g., " +
"write 42°30'00\"S as -42.5): ");
lat1 = in.nextDouble();
long1 = in.nextDouble();
lat2 = in.nextDouble();
long2 = in.nextDouble();
System.out.println();
where = selectPlanet(in);
distance = worldDistance(lat1, long1, lat1, long1, where.radius);
System.out.println();
System.out.print("Enter average speed over the ground (km/h): ");
speed = in.nextDouble();
System.out.println();
System.out.println("Shortest distance from " + origin + " to " + destination +
" on " + where + ": " + distance + " km");
System.out.println("Minimum travel time: " + formatTime(distance / speed) + "*");
System.out.println("*Ignores gravitational and atmospheric effects");
}
}
问题在于结果"距离"和"时间"都是0