Java黑皮书课后题第2章:2.15(几何:两点间距离)编写程序,提示用户输入两个点(x1,y1)和(x2,y2),显示两点距离
生活随笔
收集整理的這篇文章主要介紹了
Java黑皮书课后题第2章:2.15(几何:两点间距离)编写程序,提示用户输入两个点(x1,y1)和(x2,y2),显示两点距离
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
@TOC
題目
題目描述
2.15(幾何:兩點間距離)編寫程序,提示用戶輸入兩個點(x1,y1)和(x2,y2),顯示兩點距離
其它提示:計算兩點之間距離公式可以用Math.pow(a, 0.5)計算根下a
運行示例
Enter x1 and y1: 1.5 -3.4
Enter x2 and y2: 4 5
The distance between the two points is 8.764131445842194
代碼
import java.util.Scanner;public class Test2_15 {public static void main(String[] args) {// 獲取數(shù)據(jù)Scanner input = new Scanner(System.in);System.out.println("Enter x1 and y1: ");double x1 = input.nextDouble();double y1 = input.nextDouble();System.out.println("Enter x2 and y2: ");double x2 = input.nextDouble();double y2 = input.nextDouble();// 計算兩點距離double distance = Math.pow((Math.pow(x2-x1, 2) + Math.pow(y2-y1, 2)), 0.5);// 輸出System.out.println("The distance between the two points is " + distance);} }總結
以上是生活随笔為你收集整理的Java黑皮书课后题第2章:2.15(几何:两点间距离)编写程序,提示用户输入两个点(x1,y1)和(x2,y2),显示两点距离的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java黑皮书课后题第2章:2.12(物
- 下一篇: Java黑皮书课后题第2章:*2.17(