Java 2.15(几何:两点间距离)编写程序,提示用户输入两点(x1,y1)和(x2,y2),然后显示两点间的距离。

这个博客展示了如何使用Java编程计算平面上两点之间的距离。程序通过获取用户输入的两个点的坐标,然后应用距离公式(平方根下(x2-x1)的平方加(y2-y1)的平方)来计算它们之间的距离。示例运行输出了8.764131445842194作为两点(1.5, -3.4)和(4, 5)之间的距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

计算两点间的公式是 = \sqrt{\left ( x2 -x1)+(y2-y1\right )}

注意:可以使用Math.pow(a,0.5)来计算\sqrt{a} 

下面是一个运算示例:

Enter x1 and y1: 1.5    -3.4

Enter x1 and y2: 4   5

The distance between the two points is 8.764131445842194 

package Second;

import java.util.Scanner;

public class Code_15 {

	public static void main(String[] args) {
		System.out.print("Enter x1 and y1:");
	    Scanner input = new Scanner(System.in);
	    double x1 = input.nextDouble();
	    double y1 = input.nextDouble();
	    System.out.println("Enter x1 and y2:");
	    double x2 = input.nextDouble();
	    double y2 = input.nextDouble();
	    double a= Math.pow(x2 - x1, 2) + Math.pow(y2 - y1,2);
	    System.out.println("The distance between the two points is " + Math.pow(a,0.5));
	    
	}

}

 输出

Enter x1 and y1:1.5
-3.4
Enter x1 and y2:
4
5
The distance between the two points is 8.764131445842194

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

此生不配你情深

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值