简体   繁体   English

使用x和y坐标计算区域,如何在x和y中打破数字组?

[英]Computing area with x and y coordinates, How do I break set of numbers in x and y?

This is what I should get. 这就是我应该得到的。
Sample Output: 样本输出:

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);



    }
}

I am having trouble with for example if I enter 35.2270869 -80.8431267 , how do I separate them to x and y ? 我遇到麻烦,例如如果我输入35.2270869 -80.8431267 ,我该如何将它们分成xy

I must use the phrase: 我必须使用这句话:

Enter the GPS coordinates for the # city.

I need to separate it in order to pass them ( x and y ) to the formula. 我需要将它分开才能将它们( xy )传递给公式。

Because the values you are after are double , input.nextInt() isn't going to work, instead you should be using nextDouble . 因为你所追求的值是doubleinput.nextInt()不会起作用,相反你应该使用nextDouble

Now, because there are two values, you will need to read it twice... 现在,因为有两个值,你需要读两遍...

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

Which, if the input was 35.2270869 -80.8431267 , would assign 35.2270869 to coordinateX and -80.8431267 to coordinateY 其中,如果输入的是35.2270869 -80.8431267 ,将会分配35.2270869coordinateX-80.8431267coordinateY

You could store the values in an array for easy access... 您可以将值存储在数组中以便于访问...

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

You could even use a 2 dimensional array for storing each city... 您甚至可以使用二维数组来存储每个城市......

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...

Oh, I should point out, you will need to add in a input.nextLine() after you've read the two values to remove the carriage return/newline that is in the stream from when the user pressed Enter 哦,我应该指出,你需要在读取两个值后添加一个input.nextLine()来删除用户按Enter键时流中的回车/换行符

If you are getting 35.2270869 -80.8431267 from the command line as single line, you can not get 35.2270869 -80.8431267 as integer value. 如果您从命令行获得35.2270869 -80.8431267作为单行,则无法获得35.2270869 -80.8431267作为整数值。 Read that value as String like input.readLine(). 将该值读取为String,如input.readLine()。 Then split that String using split() method in String object( use '-' as the delimiter). 然后使用String对象中的split()方法拆分该String(使用' - '作为分隔符)。 This method is return a String array. 此方法返回一个String数组。 First element of that array is having the value of 35.2270869 and then second element of that array having the 80.8431267. 该数组的第一个元素的值为35.2270869,然后该数组的第二个元素的值为80.8431267。 Then you can get those values from the String array and convert those values as double values with Double.parseDouble(). 然后,您可以从String数组中获取这些值,并使用Double.parseDouble()将这些值转换为double值。

Good Luck !!!. 祝好运 !!!。 Try and let me know 试试让我知道

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM