我是Java的新手,我现在正在尝试不同的东西......主要是创建简单的计算器,只是为了练习使用这种语言.我的问题是,如何将响应中的小数位数限制为仅2或3个小数位,而不是我现在得到的数字.这是我为毕达哥拉斯计算器编写的代码......
基本上,我希望"回答"返回一个只有几个小数位的数字,我无法弄清楚如何做到这一点.
谢谢!
public class PythagoreanTheorem {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double numOne, numTwo, aSquared, bSquared, cSquared, answer;
System.out.println("Enter the value for Side-A: ");
numOne = sc.nextDouble();
System.out.println("Enter the value for Side-B: ");
numTwo = sc.nextDouble();
aSquared = numOne * numOne;
bSquared = numTwo * numTwo;
cSquared = aSquared + bSquared;
answer = Math.sqrt(cSquared);
System.out.println("Side-C is: " + answer);
}
}
Run Code Online (Sandbox Code Playgroud) java ×1