2014-04-26 74 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