Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java勾股定理_Java_Math - Fatal编程技术网

Java勾股定理

Java勾股定理,java,math,Java,Math,此代码生成但不显示常规输出。 我试着只显示斜边的2个小数。试着用这样的方法: import java.util.Scanner; public class PythagoreanTheorem { public static void main(String[] args) { // Get User Input Scanner keyboard = new Scanner(System.in); double side

此代码生成但不显示常规输出。
我试着只显示斜边的2个小数。

试着用这样的方法:

import java.util.Scanner; 

public class PythagoreanTheorem {

        public static void main(String[] args) {


        // Get User Input
         Scanner keyboard = new Scanner(System.in);
        double sideA, sideB, hypothenuse;


        System.out.println("Please enter the value of sideA");
        sideA = keyboard.nextDouble();
        System.out.println("Please enter the value of SideB");
        sideB = keyboard.nextDouble();

        // Find the value of the hypothenuse
        hypothenuse = Math.sqrt((sideA*sideA)+(sideB*sideB));

        double roundOff = Math.round( hypothenuse * 100) / 100;

        System.out.println("The length of the hypothenuse is "  +   roundOff );         
    }

}
运行我所拥有的代码,例如:

 double roundOff = Math.round(hypothenuse * 100.0) / 100.0;
可以使用String.format()设置十进制数的格式

 Please enter the value of sideA
 232
 Please enter the value of SideB
 454
 The length of the hypothenuse is 509.84

@DhruvBhavsar请特别注意分子和分母值,
100.0
。这项工作是否需要其他东西@DhruvBhavsar谢谢你的回答不幸的是,它没有工作。我测试了wotk@DhruvBhavsar我应该把它放在哪里替换你的System.out.println行,代码的最后一行…原来我不需要它,谢谢你的回答
System.out.printf(“抵押权的长度是%.2f%n”,抵押权)我已经解决了这个问题,不过还是要谢谢你
System.out.println("The length of the hypothenuse is "  +   String.format("%.2f", roundOff));