用java语言编写输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用java语言编写输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离。相关的知识,希望对你有一定的参考价值。
要求:输入数据有多组,每组占一行,由4个实数组成,分别表示x1,y1,x2,y2,数据之间用空格隔开。对于每组输入数据,输出一行,结果保留两位小数。
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);
追问
这个只能输入一组数据呀~怎么能多组输入呢?
追答int x1 = in.nextInt();
int y1 = in.nextInt();这是坐标1
int x2 = in.nextInt();
int y2 = in.nextInt();这是坐标2
你还要多组输入什么
我的要求上写了~~这个要求让我困惑了~~
追答你运行下我的程序就知道了 这就是多组输入,x1,y1是一组,x2,y2是一组
追问3Q~~
追答采纳一下答案 谢谢
追问有一个小问题:int distance = (int) Math.sqrt(Math.abs((x1 - x2)*(x1 - x2))+Math.abs((y1 - y2)*(y1 - y2)));
应该改为double distance = (double) Math.sqrt(Math.abs((x1 - x2)*(x1 - x2))+Math.abs((y1 - y2)*(y1 - y2)));~
#include<stdlib.h>
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;
java中关于矩形类
创建矩形类Rectangle
存储两个顶点坐标(m_x1,m_y1,m_x2,m_y2),构造方法接受2个顶点坐标的值,并调用设置方法setCoordinates将得到的坐标写入对应的属性中,设置方法setCoordinates检查坐标x和y值是否大于零,方法isRectangle()检验提供的坐标是否能构成矩形,根据检验结果设置m_isRectangle的值,并将检验结果输出到屏幕上。(有三种情况:1.两点坐标相同,构成一个点;2.两点坐标的x相同或y相同,只能构成矩形的一边;3.两点坐标分别喂矩形两个对角顶点坐标)提供计算height、width、perimeter、area的方法。加入判断方法isSquare,以确定此矩形是否为正方形。该类是公共的,保存文件名为Rectangle2.java,再编译主类Rectangle2Test测试它
属性:
private double m_height;
private double m_width;
private double m_x1;
private double m_y1;
private double m_x2;
private double m_y2;
private boolean m_isRectangle;
操作:
public Rectangle2();
public Rectangle2(double aX1,double aY1,double aX2,double aY2);
public void setCoordinates(double aX1,double aY1,double aX2,double aY2);
public boolean isRectangle();
public boolean isSquare();
private void setHeight();
private void setWidth();
public double getHeight();
public double getWidth();
public double perimeter();
public double area();
// 属性:
private double m_height;
private double m_width;
private double m_x1;
private double m_y1;
private double m_x2;
private double m_y2;
private boolean m_isRectangle;
// 操作:
public Rectangle2()
this(0.0, 0.0, 0.0, 0.0);
public Rectangle2(double aX1, double aY1, double aX2, double aY2)
setCoordinates(aX1, aY1, aX2, aY2);
public void setCoordinates(double aX1, double aY1, double aX2, double aY2)
m_x1 = aX1;
m_y1 = aY1;
m_x2 = aX2;
m_y2 = aY2;
setHeight();
setWidth();
isRectangle();
public boolean isRectangle()
if ((m_height >= 0 && m_width >= 0) && (m_x1 != m_x2 || m_y1 != m_y2))
System.err.println("输入的坐标构成一个矩形");
m_isRectangle = true;
else
if ((m_x1 == m_x2 && m_y1 != m_y2)
|| (m_x1 != m_x2 && m_y1 == m_y2))
System.err.println("输入的坐标构成一条直线");
else if (m_x1 == m_x2 && m_y1 == m_y2)
System.err.println("输入的坐标构成一点");
m_isRectangle = false;
return m_isRectangle;
public boolean isSquare()
return isRectangle() && m_height == m_width;
private void setHeight()
double side1 = Math.abs(m_x1 - m_x2);
double side2 = Math.abs(m_y1 - m_y2);
m_height = side1 >= side2 ? side1 : side2;
private void setWidth()
double side1 = Math.abs(m_x1 - m_x2);
double side2 = Math.abs(m_y1 - m_y2);
m_width = side1 <= side2 ? side1 : side2;
public double getHeight()
return m_height;
public double getWidth()
return m_width;
public double perimeter()
return 2 * (m_height + m_width);
public double area()
return m_height * m_width;
public class Rectangle2Test
public static void main(String[] args)
// TODO Auto-generated method stub
Rectangle2 rect1 = new Rectangle2();
System.err.println(rect1.isRectangle());
System.err.println(rect1.isSquare());
System.err.println(rect1.getHeight());
System.err.println(rect1.getWidth());
System.err.println(rect1.perimeter());
System.err.println(rect1.area());
Rectangle2 rect2 = new Rectangle2(22, 33, 66, 99);
System.err.println(rect2.isRectangle());
System.err.println(rect2.isSquare());
System.err.println(rect2.getHeight());
System.err.println(rect2.getWidth());
System.err.println(rect2.perimeter());
System.err.println(rect2.area());
参考技术A 这种帖子
拒绝回答
以上是关于用java语言编写输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离。的主要内容,如果未能解决你的问题,请参考以下文章