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));