我有一个Java类,并且在这个问题上遇到了困难。我们需要制作一个体积计算器。您输入球体的直径,程序会输出其体积。当我使用整数时,它可以正常工作,但是当我输入小数时,程序就会崩溃。我猜测这与变量的精度有关。
double sphereDiam;
double sphereRadius;
double sphereVolume;
System.out.println("Enter the diamater of a sphere:");
sphereDiam = keyboard.nextInt();
sphereRadius = (sphereDiam / 2.0);
sphereVolume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( sphereRadius, 3 );
System.out.println("The volume is: " + sphereVolume);
就像我说的,如果我输入一个整数,它可以正常工作。但是,当我输入25.4时,它会崩溃。