Mypoint java无法使用用户输入

时间:2014-04-26 13:12:56

标签: java

嗨,我必须写一个程序,创建两个点,并计算它们之间的距离......我已编写程序,但不是我必须用用户输入....有人请告诉我在哪里我我错了?

class MyPoint {

    private double x;
    private double y;

    public double getx()
    {
        return x;
    }
    public double gety()
    {
        return y;
    }
    public MyPoint()
    {

    }

    public MyPoint(double x, double y)
    {
         this.x = x;
         this.y = y;
    }
    public double distance(MyPoint secondPoint) {
        return distance(this, secondPoint);
      }

      public static double distance(MyPoint p1, MyPoint p2) {
        return Math.sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y)
            * (p1.y - p2.y));
      }
}

public class MyPointTest
{
    public static void main(String[] args)
        {
           MyPoint p1 = new MyPoint(0,0);
           MyPoint p2 = new MyPoint(10, 30.5);
           p1.distance(p2);
           System.out.println("Distance between two points (0,0) and (10,30.5)= "+MyPoint.distance(p1,p2));
        }
}

这是我尝试过的用户输入

import java.util.Scanner;
public class TestMyPoint {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        MyPoint p1 = new MyPoint();
            MyPoint p2 = new MyPoint();
        System.out.print("Enter a  first point  " + p1 );
        System.out.print("Enter a  second point  " + p2 );
        System.out.println(p1.distance(p2));
        System.out.println(MyPoint.distance(p1, p2));

    }

}

1 个答案:

答案 0 :(得分:2)

扫描仪是一个良好的开端。 尝试类似:

System.out.println("Please enter x of the first point:");
double x1 = input.nextDouble();
System.out.println("Please enter y of the first point:");
double y1 = input.nextDouble();
MyPoint p1 = new MyPoint(x1, y1);
...