Java 计算两个给定点之间的距离

Java 计算两个给定点之间的距离,java,class,distance,Java,Class,Distance,我必须创建一个类来计算类中两个给定点之间的距离。老师给了我们作业的上半部分,包括所有必要的代码,没有修改,我遇到的问题是创建课堂部分。这就是我目前所拥有的 class Point{ int x; int y; public Point(){ this.x = 0; this.y = 0; } public Point(int x, int y){ this.x = x; this.y = y; } public double distance(int x,

我必须创建一个类来计算类中两个给定点之间的距离。老师给了我们作业的上半部分,包括所有必要的代码,没有修改,我遇到的问题是创建课堂部分。这就是我目前所拥有的

class Point{
int x;
int y;

public Point(){
    this.x = 0;
    this.y = 0;

}

public Point(int x, int y){
    this.x = x;
    this.y = y;
}
 public double distance(int x, int y) {
    double d = Math.sqrt( Math.pow(x2-x1, 2) + Math.pow(y2-y1, 2) );
    return distance;
}
}
作业的上半部分如下所示:

import java.util.Scanner;


class Assignment4{
public static void main(String[] args){
    // first and second points
    Point first, second;

    // try parsing points from command line args
    if(args.length==4){
        // new Point(int x, int y) creates a new Point located at position (x,y)
        first = new Point(Integer.valueOf(args[0]), Integer.valueOf(args[1]));
        second = new Point(Integer.valueOf(args[2]), Integer.valueOf(args[3]));
    }

    // if not specified as argument, get points from user
    else{
        Scanner input = new Scanner(System.in);

        System.out.println("Enter first point: ");
        first = new Point(input.nextInt(),input.nextInt());

        System.out.println("Enter second point: ");
        second = new Point(input.nextInt(),input.nextInt());
    }

    //calculate distance
    //double d = Math.sqrt( Math.pow(x2-x1, 2) + Math.pow(y2-y1, 2) );
    double d = first.distance(second.x, second.y);

    System.out.println("Distance between " + 
        "(" + first.x + "," + first.y + ")" + 
        " and " + 
        "(" + second.x + "," + second.y + ")" + 
        " is " + d);
    System.out.println();
}
}
当我尝试编译程序时,它说找不到表示x2、x1、y2、y1和距离的符号

这里:

class Point{
     int x;
     int y;

     .....
     .....

     public double distance(int x, int y) {
        double d = Math.sqrt( Math.pow(x2-x1, 2) + Math.pow(y2-y1, 2) );  //ERROR IN THIS LINE
        return distance; //ERROR HERE TOO...(2)
     }
}
类或方法参数中未定义x1、x2、y1、y2

将其与以下行交换:

double d=Math.sqrt Math.powthis.x-x,2+Math.powthis.y-y,2

二, 错误2与此行交换:

返回d


@ChthonicProject Not really distance是一种实例方法,它试图计算当前点与坐标由两个参数给出的点之间的距离。距离的定义,即公共双距离int x,int y,是作为赋值的一部分提供给您的,还是您的想法?我只是想知道为什么这个方法必须取x和y,而不是另一个点。谢谢!这帮了大忙。但是当我使用这个固定部分时,我得到了另一个与以下内容相关的错误:first=newpointinteger.valueOfargs[0],Integer.valueOfargs[1];第二个=新的PointInteger.valueOfargs[2],Integer.valueOfargs[3];}当我能够在homesure上运行时,我将获得详细的错误信息。没有问题:。我不理解您所说的关于固定部分的内容,我也不明白,如果您将有效参数传递给它,为什么代码的该部分会给出错误。把错误信息发给我,我就可以帮你了。