Golang程序 寻找两个物体间的引力
操作步骤
- 读取质量和质量之间的距离,并将其存储在不同的变量中。
- 将其中一个变量初始化为重力常数G的值。
- 然后,用公式f=(Gm1m2)/(r**2)来确定质量之间的作用力。
- 四舍五入到小数点后两位,打印出力的数值。
输入第一个质量:1000000
输入第二个质量。500000
输入质量中心之间的距离:20
因此,引力是:0.08 N
输入第一个质量:90000000
输入第二个质量:7000000
输入质量中心之间的距离:20
因此,引力是:105.1 N
解释
- 用户必须输入质量和质量之间的距离的值,并将它们存储在不同的变量中。
- 其中一个变量被初始化为重力常数(G)的值,等于6.673*(10**-11)。
- 然后,公式:f=(Gm1m2)/(r**2),其中m1和m2是质量,r是它们之间的距离,用来确定作用于质量之间的力的大小。
- 计算出的力被四舍五入到小数点后2位并打印出来。
例子
package main
import (
"fmt"
"math"
)
func main(){
var a, b, r float64
fmt.Print("Enter the first mass: ")
fmt.Scanf("%f", &a)
fmt.Print("Enter the second mass: ")
fmt.Scanf("%f", &b)
fmt.Print("Enter the distance between the centres of the masses: ")
fmt.Scanf("%f", &r)
G:=6.673*(math.Pow(10, -11))
f:=(G*a*b)/(math.Pow(r, 2))
fmt.Printf("Hence, the gravitational force is: %.2f N", f)
}
输出
Enter the first mass: 1000000
Enter the second mass: 500000
Enter the distance between the centres of the masses: 20
Hence, the gravitational force is: 0.08 N
极客教程