輸入兩點坐標(X1,Y1),(X2,Y2),計算並輸出兩點間的距離。輸入數據有多組,每組占一行,由4個實數組成,分別表示x1,y1,x2,y2,數據之間用空格隔開。 例如輸入: 1 3 4 6 則輸出:4.24
注意:對於每組輸入數據,輸出一行,結果保留兩位小數。
package fifth; import java.util.Scanner; public class 平方根 { public static void main(String[] args) { System.out.println("請輸入兩點坐標:"); Scanner sc=new Scanner(System.in); double x1,x2,y1,y2; double l; while (sc.hasNext()){ x1=sc.nextDouble(); y1=sc.nextDouble(); x2=sc.nextDouble(); y2=sc.nextDouble(); l=Math.sqrt(Math.pow((x1-x2),2)+Math.pow((y1-y2),2));//Math.sqrt返回一個數的平方根,Math.pow返回一個數的平方 System.out.println("兩點之間的距離:"+String.format("%.2f",l)); } } }