JAVA表示点的坐标并且用math.sqrt或math.hypot来计算两点间的距离

1.math.sqrt
import sheffield.*;
public class w {
public static void main(String[] args) {

    EasyReader keyboard = new EasyReader();
   
    double a = keyboard.readDouble("Please type first: ");
  
    double b = keyboard.readDouble("Please type second: ");
   
    double c = keyboard.readDouble("Please type third: ");
  
    double d = keyboard.readDouble("Please type fourth: ");

    System.out.println("pointA(" + a + "," + b + ")");
    System.out.println("pointB(" + c + "," + d + ")");
    double sum;
    sum = Math.sqrt((a - c) * (a - c) + (b - d) * (b - d));
    System.out.println(sum);
}

}
2.math.hypot
Math.hypot() 函数返回它的所有参数的平方和的平方根
import sheffield.*;

public class w {
public static void main(String[] args) {

    EasyReader keyboard = new EasyReader();
    double a = keyboard.readDouble("Please type first: ");
    double b = keyboard.readDouble("Please type second: ");
    double c = keyboard.readDouble("Please type third: ");
    double d = keyboard.readDouble("Please type fourth: ");

    System.out.println("pointA(" + a + "," + b + ")");
    System.out.println("pointB(" + c + "," + d + ")");
    double sum;

    sum = Math.hypot(a - c, b - d);
    System.out.println(sum);

}

}

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐