英文:
Volume of Hemi-Sphere printing as zero
问题
我创建了一个使用Go语言计算半球体积的程序,但是程序输出的半球体积为零:
package main
import (
"fmt"
"math"
)
func volumeHemisphere(radius float64) float64 {
return 2 / 3 * math.Pi * math.Pow(radius, 3)
}
func main() {
fmt.Println(volumeHemisphere(2.0))
}
英文:
I created a program to calculate volume of hemisphere using go ,the program is printing the volume as zero :
package main
import (
"fmt"
"math"
)
func volumeHemisphere(radius float64) float64 {
return 2 / 3 * math.Pi * math.Pow(radius, 3)
}
func main() {
fmt.Println(volumeHemisphere(2.0))
}
答案1
得分: 2
将代码修改为float64(2)/float64(3)* math.Pi * math.Pow(radius, 3)
。
package main
import (
"fmt"
"math"
)
func volumeHemisphere(radius float64) float64 {
return float64(2) / float64(3) * math.Pi * math.Pow(radius, 3)
}
func main() {
fmt.Println(volumeHemisphere(2))
}
输出结果为:
16.755160819145562
英文:
Modify your code as float64(2)/float64(3)* math.Pi * math.Pow(radius, 3)
package main
import (
"fmt"
"math"
)
func volumeHemisphere(radius float64) float64 {
return float64(2) / float64(3) * math.Pi * math.Pow(radius, 3)
}
func main() {
fmt.Println(volumeHemisphere(2))
}
Output:
16.755160819145562
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论