2014-04-26 47 views
0

嗨我必须编写一个程序,创建两个点,并计算它们之间的距离...我已经编写的程序,但不是我必须做用户输入....有人能告诉我我要去哪里吗?Mypoint的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)); 

    } 

} 
+0

你会得到什么结果? –

+0

输入第一个关键点MyPoint @ 49c7e176输入第二个关键点[email protected] 32.09750769140807 – user3376176

+0

“*我必须使用用户输入*”,那么为什么不从用户读取数据呢? – Pshemo

回答

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); 
... 
+0

它的工作原理...非常感谢你...非常感谢 – user3376176