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 );
}
}
此代码构建但不显示常规输出。 我试图从斜边只显示2位小数。
答案 0 :(得分:1)
尝试使用以下内容:
double roundOff = Math.round(hypothenuse * 100.0) / 100.0;
运行我的代码:
Please enter the value of sideA
232
Please enter the value of SideB
454
The length of the hypothenuse is 509.84
答案 1 :(得分:0)
您可以使用String.format()格式化十进制数
System.out.println("The length of the hypothenuse is " + String.format("%.2f", roundOff));