所以我的任务是创建一个名为city的对象并拥有城市的经度,纬度和名称,然后询问城市的数量,然后将用户输入的名称,经度和纬度输入到数组中以供访问。我将如何用现在的东西来实现这一目标?
import java.util.Scanner;
public class Assignment3 {
public static void main(String[] args) {
Scanner lat = new Scanner(System.in); //for double data
Scanner lon = new Scanner(System.in); //for double data
Scanner city = new Scanner(System.in); //for string data
Scanner in = new Scanner(System.in); //for int
System.out.print("How many cities? ");
int number = in.nextInt();
City [] cities = new City [number];
for(int i = 1; i <= number; i++){
System.out.println("City # " + i);
System.out.print("Enter name: " );
String name = city.Next();
System.out.print("Enter longitude: ");
double longitude = lon.nextDouble();
System.out.println("Enter latitude: ");
double latitude = lat.nextDouble();
}
}
}
public class City {
String name; //name of cities
double lon, lat; //longitude & latitude
City(String name, double lon, double lat)
{
this.name = name;
this.lon = lon;
this.lat = lat;
}
public void report(){ //should report current position of a city
System.out.println("City: " + this.name); // displays city name
System.out.println("Longitude: " + this.lon); //displays longitude
System.out.println("Latitude: " + this.lat); //displays latitude
}
public double distanceFrom(City otherCity){ //should calculate the distance
return 0;
}
}
关键是,您可以询问城市的直接位置,然后询问它们之间的距离。非常感谢任何帮助,谢谢
答案 0 :(得分:0)
不确定我是否理解了您的问题,但您需要使用您在for循环中收集的当前信息创建城市对象。然后将其保存在您的城市数组中。
for(int i = 0; i < number; i++){
System.out.println("City # " + (i+1));
System.out.print("Enter name: " );
String name = city.Next();
System.out.print("Enter longitude: ");
double longitude = lon.nextDouble();
System.out.println("Enter latitude: ");
double latitude = lat.nextDouble();
City a_city = new City(name,longitude,latitude);
cities[i] = a_city;
}
我将你的for循环从i = 1更改为i = 0
要查找两个城市之间的距离,您可能需要查看此帖子 Calculate distance between two latitude-longitude points? (Haversine formula)
答案 1 :(得分:0)
您可以通过这种简单的方法实现这一目标:
将City
类的setter实例方法定义到您希望用户输入的每个属性,然后在主程序中迭代城市的每个对象并设置其信息:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("How many cities? ");
int number = input.nextInt();
City [] cities = new City [number];
for(int i = 0; i < number; i++){
cities[i] = new City(); // create new object of City
System.out.println("City # " + (i+1));
System.out.print("Enter name: " );
String name = input.next();
cities[i].setName(name);
System.out.print("Enter longitude: ");
double longitude = input.nextDouble();
cities[i].setLongitude(longitude);
System.out.print("Enter latitude: ");
double latitude = input.nextDouble();
cities[i].setLatitude(latitude);
}
}
public class City {
private String name; //name of cities
private double lon, lat; //longitude & latitude
City(){ // default constructor
name="";
this.lon = 0;
this.lat = 0;
}
City(String name, double lon, double lat)
{
this.name = name;
this.lon = lon;
this.lat = lat;
}
public void setName(String n){
name = n;
}
public void setLongitude(double longitude){
lon = longitude;
}
public void setLatitude(double latitude){
lat = latitude;
}
}