import java.util.Scanner;
public class Chpt4_Project {
private static Scanner input;
public static void main(String[] args) {
input = new Scanner(System.in);
System.out.print("Enter the first city: ");
// Prompt user for city
String city1 = input.nextLine();
System.out.print("Enter the second city: ");
// Prompt user for the second city
String city2 = input.nextLine();
System.out.print("Enter the latitude and longitude of the first city separated by a space (Ex. 28.8 81.2): ");
String coord1 = input.toString();
// Save coordinates as a string
String [] tempArray1 = coord1.substring(coord1.indexOf("(")+1, coord1.lastIndexOf(")")).split(" ");
// Split string
double lat1 = Double.parseDouble(tempArray1[0]);
double long1 = Double.parseDouble(tempArray1[1]);
System.out.print("Enter the latitude and longitude of the second city separated by a space (Ex. 28.8 81.2): ");
String coord2 = input.toString();
// Save coordinates as a string
String [] tempArray2 = coord2.substring(coord2.indexOf("(")+1, coord2.lastIndexOf(")")).split(" ");
// Split string
double lat2 = Double.parseDouble(tempArray2[0]);
double long2 = Double.parseDouble(tempArray2[1]);
double x = (long1 - long2) * 55;
double y = (lat1 - lat2) * 69;
double distance = Math.sqrt(x*x+y*y);
System.out.println("The distance between " + city1 + city2 + " is " + distance + "miles.");
}
}
入门编码的学生,我正在尝试编写一个程序,该程序将告诉我两个城市坐标之间的距离。但是,当我尝试将字符串拆分为经度和纬度时,我似乎无法获得{ {1}}对。
这是结果,在此先感谢!
tempArrays
答案 0 :(得分:0)
解决了尝试在代码下运行的问题
import java.util.Scanner;
public class Main{
private static Scanner input;
public static void main(String []args){
input = new Scanner(System.in);
System.out.print("Enter the first city: ");
// Prompt user for city
String city1 = input.nextLine();
System.out.print("Enter the second city: ");
// Prompt user for the second city
String city2 = input.nextLine();
System.out.print("Enter the latitude and longitude of the first city separated by a space (Ex. 28.8 81.2): ");
String coord1 = input.nextLine();
// Save coordinates as a string
String [] tempArray1 = coord1.split(" ");
// Split string
double lat1 = Double.parseDouble(tempArray1[0]);
double long1 = Double.parseDouble(tempArray1[1]);
System.out.print("Enter the latitude and longitude of the second city separated by a space (Ex. 28.8 81.2): ");
String coord2 = input.nextLine();
// Save coordinates as a string
String [] tempArray2 = coord2.split(" ");
// Split string
double lat2 = Double.parseDouble(tempArray2[0]);
double long2 = Double.parseDouble(tempArray2[1]);
double x = (long1 - long2) * 55;
double y = (lat1 - lat2) * 69;
double distance = Math.sqrt(x*x+y*y);
System.out.println("The distance between " + city1 + city2 + " is " + distance + "miles.");
}
}
答案 1 :(得分:0)
您的问题是,在查询纬度和经度时,您没有阅读新的输入内容。您实际上是在将Scanner
对象转换为字符串,然后尝试解析它,这显然不是您的目标。只需按如下所示从用户那里读取新输入即可:
System.out.print("Enter the latitude and longitude of the first city separated by a space (Ex. 28.8 81.2): ");
String coord1 = input.toString(); //Needs to be replaced
第二行应替换为input.nextLine();
第二个要求纬度和经度的代码也是如此。