English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Java Math hypot()方法计算x2 + y2的平方根(即,斜边)并将其返回。
hypot()方法的语法为:
Math.hypot(double x, double y)
注意:hypot()方法是静态方法。因此,我们可以使用类名Math直接调用该方法。
x,y - 双精度类型参数
返回Math.sqrt(x 2 + y 2)
返回的值应在double数据类型的范围内。
注意:Math.sqrt()方法返回指定参数的平方根。要了解更多信息,请访问Java Math.sqrt()。
class Main { public static void main(String[] args) { //创建变量 double x = 4.0; double y = 3.0; //计算 Math.hypot() System.out.println(Math.hypot(x, y)); // 5.0 {} {}
class Main { public static void main(String[] args) { //三角形的边 double side1 = 6.0; double side2 = 8.0; // بناءً على نظرية بيتاغورس // hypotenuse = (side1)^2 + (side2)^2 double hypotenuse1 = (side1) *(side1) + (side2) * (side2); System.out.println(Math.sqrt(hypotenuse1)); // العودة 10.0 // حساب الضلع المستقيم باستخدام Math.hypot() // Math.hypot() يقدم √((side1)^2 + (side2)^2) double hypotenuse2 = Math.hypot(side1, side2); System.out.println(hypotenuse2); // العودة 10.0 {} {}
في المثال أعلاه، نستخدم Math.hypot() وفقرة بيتاغورس لحساب ضلع المثلث.