2014-10-27 65 views
1

这是我应该得到的。
样本输出:带有x和y坐标的计算区域,如何在x和y中打破一组数字?

Please enter the coordinates in a clockwise order. 
    Enter the GPS coordinates for the 1st city: 35.2270869 -80.8431267 
    Enter the GPS coordinates for the 2nd city: 32.0835407 -81.0998342 
    Enter the GPS coordinates for the 3rd city: 28.5383355 -81.3792365 
    Enter the GPS coordinates for the 4th city: 33.7489954 -84.3879824 
    The area is: 117863.342 
import java.util.Scanner; 

public class problem { 
    public static void main(String[] args) { 

     Scanner input = new Scanner(System.in); 

     System.out.println("Please enter the coordinates in a clowise order"); 
     System.out.println("Enter the GPS coordinates for the 1st city: "); 
     double coordinateOne= input.nextInt(); 

     System.out.println("Enter the GPS coordinates for the 1st city: "); 
     double coordinateTwo= input.nextInt(); 

     System.out.println("Enter the GPS coordinates for the 1st city: "); 
     double coordinateThree= input.nextInt(); 

     System.out.println("Enter the GPS coordinates for the 1st city: "); 
     double coordinateFour= input.nextInt(); 

     double earthRadius= 6371.01; 

     //Get distance 


     //distance=(radius)arccos(sin(x1)sin(x2)+cos(x1)cos(x2)cos(y1−y2)) 
     double distance= (earthRadius)*Math.acos(Math.sin(coordinateOne)*Math.sin(coordinateTwo)) 
       + Math.cos(coordinateOne)*Math.cos(coordinateTwo); 

//  System.out.println("The area is: "+distance); 



    } 
} 

我有,例如,如果我进入35.2270869 -80.8431267麻烦,我如何将它们分开来xy

我必须使用短语:

Enter the GPS coordinates for the # city. 

我需要它,以便分离,以它们(xy)传递给该公式。

回答

3

因为您以后的值是double,input.nextInt()不起作用,而应该使用nextDouble

现在,因为有两个值,则需要读了两遍......

double coordinateX = input.nextDouble(); 
double coordinateY = input.nextDouble(); 

其中,如果输入的是35.2270869 -80.8431267,将分配给35.2270869coordinateX-80.8431267coordinateY

您可以将值存储在数组中以便于访问...

double city1[] = new double[2]; 
city[0] = input.nextDouble(); 
city[1] = input.nextDouble(); 

您可以ev连接使用二维数组存储每个城市...

double coordinates[][] = new double[4][2]; 
// City #1 
coordinates[0][0] = input.nextDouble(); 
coordinates[0][1] = input.nextDouble(); 

// City #2 
coordinates[1][0] = input.nextDouble(); 
coordinates[1][1] = input.nextDouble(); 
//...etc... 

哦,我要指出,你需要在input.nextLine()添加您已经阅读了两个值后,除去回车/换行符这是在流中从当用户按下输入

0

如果您在命令行作为单行越来越35.2270869 -80.8431267,你不能得到35.2270869 -80.8431267为整数值。像input.readLine()那样读取该值为String。然后使用String对象中的split()方法分割该String(使用' - '作为分隔符)。这个方法返回一个String数组。该数组的第一个元素的值为35.2270869,然后该数组的第二个元素的值为80.8431267。然后,您可以从String数组中获取这些值,并使用Double.parseDouble()将这些值转换为双精度值。

祝你好运!!!试着让我知道

+0

如果他们输入'35.2270869 80.8431267'而不是... – MadProgrammer 2014-10-27 05:39:33

+0

您可以使用空格字符作为split()方法的分隔符。我认为SPACE字符是split()分隔符的最好的字符。 – 2014-10-27 05:41:40

+0

@MadProgrammer答案更简单。 :) – 2014-10-27 05:42:27