Dr Java帮助输入的公式不起作用
//在以下代码行中,要求用户输入长度以确定规则二十面体的体积,但是,当输入时,程序始终输出0.0作为体积的答案Dr Java帮助输入的公式不起作用,java,Java,//在以下代码行中,要求用户输入长度以确定规则二十面体的体积,但是,当输入时,程序始终输出0.0作为体积的答案 import java.io.*; //allows I/o statements class VolumeIcosahedron //creating the 'volumeIcosahedron' class { //allows strings with exceptions to IO = input/output public static void main (S
import java.io.*; //allows I/o statements
class VolumeIcosahedron //creating the 'volumeIcosahedron' class
{
//allows strings with exceptions to IO = input/output
public static void main (String[] args) throws IOException
{
BufferedReader myInput = new BufferedReader(
new InputStreamReader (System.in)); //system input/ output
String stringNum; // the number string
double V; // integer with decimals volume
int L; // integer required length
//System output
System.out.println("Hello, what is the required length");
stringNum = myInput.readLine();
L = Integer.parseInt(stringNum);
V = 5/12 *(3 + Math.sqrt(5))*(L*L*L);
System.out.println("The volume of the regular Icosahedron is " + V);
}
}
因为整数中的
5/12
等于0
,所以它总是导致0
尝试使用5.0
强制除法,但不涉及整数除法
V = 5.0/12 *(3.0 + Math.sqrt(5))*(L*L*L);
因为整数中的
5/12
等于0
,所以它总是导致0
尝试使用5.0
强制除法,但不涉及整数除法
V = 5.0/12 *(3.0 + Math.sqrt(5))*(L*L*L);
我认为这是一条令人不快的线:
V = 5/12 *(3 + Math.sqrt(5))*(L*L*L);
5/12返回一个int
(整数),它总是被截断为0,因此0*任何东西都将返回0
将其更改为此,使用字母d表示这些数字为double类型:
V = 5d/12d *(3 + Math.sqrt(5))*(L*L*L);
我认为这是一条令人不快的线:
V = 5/12 *(3 + Math.sqrt(5))*(L*L*L);
5/12返回一个int
(整数),它总是被截断为0,因此0*任何东西都将返回0
将其更改为此,使用字母d表示这些数字为double类型:
V = 5d/12d *(3 + Math.sqrt(5))*(L*L*L);
原因是您在计算中使用的是整数。 对于整数,您应该将除法视为欧几里德运算,即a=bq+r。 所以在你的程序中,5/12总是返回0(5=0*12+5) 如果将行更改为如下所示(将每个整数替换为双精度):
那么结果就会不同。原因是您在计算中使用的是整数。 对于整数,您应该将除法视为欧几里德运算,即a=bq+r。 所以在你的程序中,5/12总是返回0(5=0*12+5) 如果将行更改为如下所示(将每个整数替换为双精度): 那么结果就不一样了