Java 代码实现勾股定理计算斜边长度
以下是使用 Java 语言编写的勾股定理代码:
import java.util.Scanner;
public class PythagoreanTheorem {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the length of side a: ");
double a = input.nextDouble();
System.out.print("Enter the length of side b: ");
double b = input.nextDouble();
double c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
System.out.println("The length of the hypotenuse is " + c);
}
}
在这个程序中,我们首先创建了一个 Scanner 对象,用于从控制台读取用户输入。然后,我们提示用户输入两条直角边的长度,将这些值存储在变量 a 和 b 中。接下来,我们使用 Math 类的 sqrt 方法和 pow 方法来计算斜边 c 的长度,并将结果存储在变量 c 中。最后,我们将斜边的长度输出到控制台。
原文地址: https://www.cveoy.top/t/topic/m8I0 著作权归作者所有。请勿转载和采集!