Golang 程序,用于求给定所有三个边的三角形的面积

步骤

  • 读取三角形的所有三个边并将它们存储在三个单独的变量中。

  • 使用 Heron 公式,计算三角形的面积。

  • 打印三角形的面积。

Enter first side: 15
Enter second side: 9
Enter third side: 7
Area of the triangle is: 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("输入三角形的第一边: ")
   fmt.Scanf("%f", &a)
   fmt.Print("输入三角形的第二边: ")
   fmt.Scanf("%f", &b)
   fmt.Print("输入三角形的第三边: ")
   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)
}
输出结果
输入三角形的第一边: 15
输入三角形的第二边: 9
输入三角形的第三边: 7
Area of the triangle is: 20.69