gpt4 book ai didi

java - Dr Java 帮助输入的方程不起作用

转载 作者:行者123 更新时间:2023-12-02 00:00:29 25 4
gpt4 key购买 nike

//在下面的代码行中,要求用户输入长度来确定正二十面体的体积,但是,当输入时,程序总是输出 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 (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);  

关于java - Dr Java 帮助输入的方程不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14942443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com