用java语言编写输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离。

2024-12-12 17:16:53
推荐回答(2个)
回答1:

import java.util.Scanner;
public class TestObject {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.println("请输入第一个坐标点:");
int x1 = in.nextInt();
int y1 = in.nextInt();
System.out.println("请输入第二个坐标点:");
int x2 = in.nextInt();
int y2 = in.nextInt();
int distance = (int) Math.sqrt(Math.abs((x1 - x2)*(x1 - x2))+Math.abs((y1 - y2)*(y1 - y2)));
System.out.println("两点间距离是:"+distance);
}
}

回答2:

#include
#include
int main()
{
double x1,x2,y1,y2,d;
while(scanf("%lf %lf %lf",&x1,&x2,&y1,&y2)!=EOF)
{
d=(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
d=pow(d,0.5);
}
return 0;
}