半球的体积打印为零。

huangapple go评论65阅读模式
英文:

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

huangapple
  • 本文由 发表于 2021年9月25日 03:18:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/69320130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定