2017-10-21 95 views
-4

我正在練習我很快就會進行測試,我知道它的主要部分將使用距離公式。我的代碼運行,但我不能幫助,但覺得我得到了錯誤的輸出。距離公式校正

import java.lang.*; 
public class Distance 
{ 
    public static void main(String[] args) 
    { 
     double x1=4; 
     double x2=6; 
     double y1=4; 
     double y2=10; 
     double distance =Math.sqrt(Math.pow(y2,-y1) + Math.pow(x2,-x1)); 
     System.out.println(distance); 
    } 
} 
+0

沒錯這是錯的,看到的是https ://brilliant.org/wiki/distance-formula/#distance-between-two-points – 2017-10-21 18:34:31

+3

我投票結束這個問題作爲題外話,因爲它是關於數學 – 2017-10-21 18:34:44

+0

你不需要導入'java.lang '。 – khelwood

回答

0

Math.pow需要兩個參數 - 基和指數。在公式你方的差異,所以它應該是這樣的:

double distance = Math.sqrt(Math.pow(y2 - y1, 2) + Math.pow(x2 - x1, 2)); 
//         ^^^^^^^    ^^^^^^^ 
2

公式中的問題應該是:

import java.lang.*; 
public class Distance 
{ 
     public static void main(String[] args) { 
     double x1 = 4; 
     double x2 = 6; 
     double y1 = 4; 
     double y2 = 10; 
     double distance = Math.sqrt(Math.pow((y2 - y1), 2) + Math.pow((x2 - x1), 2)); 
     System.out.println(distance); 
    } 

} 

enter image description here