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));
}
}
你會得到什麼結果? –
輸入第一個關鍵點MyPoint @ 49c7e176輸入第二個關鍵點[email protected] 32.09750769140807 – user3376176
「*我必須使用用戶輸入*」,那麼爲什麼不從用戶讀取數據呢? – Pshemo