Golang程序 在给定的三条边上求三角形的面积
操作步骤
- 读取三角形的所有三条边,并将它们存储在三个独立的变量中。
- 使用Heron公式,计算三角形的面积。
- 打印三角形的面积。
输入第一边:15
输入第二边:9
输入第三边:7
三角形的面积是:20.69
输入第一边:5
输入第二边:6
输入第三边:7
三角形的面积是:14.7
解释
- 用户必须输入所有三个数字,并将它们存储在不同的变量中。
- 首先,找到s的值,等于(a+b+c)/2。
- 然后,应用赫伦公式来确定所有三条边所形成的三角形的面积。
- 最后,打印出该三角形的面积。
例子
package main
import (
"fmt"
"math"
)
func main(){
var a, b, c float64
fmt.Print("Enter first side of the triangle: ")
fmt.Scanf("%f", &a)
fmt.Print("Enter second side of the triangle: ")
fmt.Scanf("%f", &b)
fmt.Print("Enter third side of the triangle: ")
fmt.Scanf("%f", &c)
s:=(a+b+c)/2
area:=math.Sqrt(s * (s - a) * (s - b) * (s - c))
fmt.Printf("Area of the triangle is: %.2f", area)
}
输出
Enter first side of the triangle: 15
Enter second side of the triangle: 9
Enter third side of the triangle: 7
Area of the triangle is: 20.69
极客教程