GolangStudy-004-BMI计算器
Posted DreamLife.
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GolangStudy-004-BMI计算器相关的知识,希望对你有一定的参考价值。
本文主要实现BIM计算,包括多个人的平均值,总共有两个版本,第一个版本就是最开始学习C语言的时候的那种,在一个Main函数里实现所有,第二个版本就是把功能分开,封装成了函数。
关键字:
BIM
、体脂
、计算器
、Golang
、平均值
BMI计算器
第一版本代码
这个版本的代码,就是我们最开始学习语言的那种,面向过程的思想,一个Main函数走到黑。
/*
BMI = 体重(公斤) / (身高*身高)(米)
体制率: (1.2 * BMI + 0.23*年龄 - 5.4 -10.8*性别(男1女0)) / 100
*/
package main
import "fmt"
func main() {
fmt.Println("BMI is Running…")
var(
weight float64 //体重(公斤)
height float64 //身高(米)
age int //年龄
sex string
sexRate float64
avearageFat float64 = 0.0 //平均体脂
peoplecount int = 0
)
for isContinue := true; isContinue; {
fmt.Printf("性别:")
fmt.Scanln(&sex)
fmt.Printf("体重(公斤):")
fmt.Scanln(&weight)
fmt.Printf("身高(米):")
fmt.Scanln(&height)
fmt.Printf("年龄:")
fmt.Scanln(&age)
if sex == "man" {
sexRate = 1
}else{
sexRate = 0
}
peoplecount ++
var bmi float64 = weight/(height * height)
var fatRate float64 = (1.2 * bmi + 0.23 * float64(age) - 5.4 - 10.8 * sexRate) /100
// fmt.Printf("BMI:%2.3f 体脂率:%2.3f",bmi,fatRate) //输出BMI 体脂率
fmt.Println("+**************************************+")
if sex == "man" {
if age >= 18 && age <=39 {
if fatRate <= 0.1 {
fmt.Printf("BMI:%2.3f\\n 体脂率:%2.3f\\n太瘦了\\n",bmi,fatRate)
}else if fatRate >0.1 &&fatRate <= 0.16 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n标准,太棒了\\n",bmi,fatRate)
}else if fatRate > 0.16 && fatRate <= 0.21 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n偏重,不影响把妹,加油\\n",bmi,fatRate)
}else if fatRate > 0.21 && fatRate <= 0.26 {
fmt.Printf("BMI:%2.3f\\n 体脂率:%2.3f\\n稍微有点胖了,需要控制\\n",bmi,fatRate)
}else {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n危险!危险!危险!要减肥了\\n",bmi,fatRate)
}
}else if age >= 40 && age <= 59{
if fatRate <= 0.11 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n太瘦了\\n",bmi,fatRate)
}else if fatRate >0.11 &&fatRate <= 0.17 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n标准,太棒了\\n",bmi,fatRate)
}else if fatRate > 0.17 && fatRate <= 0.22 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n偏重,不影响把妹,加油\\n",bmi,fatRate)
}else if fatRate > 0.22 && fatRate <= 0.27 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n稍微有点胖了,需要控制\\n",bmi,fatRate)
}else {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n危险!危险!危险!要减肥了\\n",bmi,fatRate)
}
}else{
if fatRate <= 0.13 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n太瘦了\\n",bmi,fatRate)
}else if fatRate >0.13 &&fatRate <= 0.19 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n标准,太棒了\\n",bmi,fatRate)
}else if fatRate > 0.19 && fatRate <= 0.24 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n偏重,不影响把妹,加油\\n",bmi,fatRate)
}else if fatRate > 0.24 && fatRate <= 0.29 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n稍微有点胖了,需要控制\\n",bmi,fatRate)
}else {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n危险!危险!危险!要减肥了\\n",bmi,fatRate)
}
}
}else{
if age >= 18 && age <=39 {
if fatRate <= 0.2 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n太瘦了\\n",bmi,fatRate)
}else if fatRate >0.2 &&fatRate <= 0.27 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n标准,太棒了\\n",bmi,fatRate)
}else if fatRate > 0.27 && fatRate <= 0.34 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n偏重,不影响,加油\\n",bmi,fatRate)
}else if fatRate > 0.34 && fatRate <= 0.39 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n稍微有点胖了,需要控制\\n",bmi,fatRate)
}else {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n危险!危险!危险!要减肥了\\n",bmi,fatRate)
}
}else if age >= 40 && age <= 59{
if fatRate <= 0.21 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n太瘦了\\n",bmi,fatRate)
}else if fatRate >0.21 &&fatRate <= 0.28 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n标准,太棒了\\n",bmi,fatRate)
}else if fatRate > 0.28 && fatRate <= 0.35 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n偏重,不影响,加油\\n",bmi,fatRate)
}else if fatRate > 0.35 && fatRate <= 0.40 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n稍微有点胖了,需要控制\\n",bmi,fatRate)
}else {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n危险!危险!危险!要减肥了\\n",bmi,fatRate)
}
}else{
if fatRate <= 0.22 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n太瘦了\\n",bmi,fatRate)
}else if fatRate >0.22&&fatRate <= 0.29 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n标准,太棒了\\n",bmi,fatRate)
}else if fatRate > 0.29&& fatRate <= 0.36 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n偏重,不影响,加油\\n",bmi,fatRate)
}else if fatRate > 0.36 && fatRate <= 0.41 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n稍微有点胖了,需要控制\\n",bmi,fatRate)
}else {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n危险!危险!危险!要减肥了\\n",bmi,fatRate)
}
}
}
fmt.Printf("+**************************************+\\n共录入%d人记录,平均体脂为%2.3f \\n+**************************************+\\n",peoplecount,(avearageFat*float64(peoplecount-1)+fatRate)/float64(peoplecount))
//循环控制
fmt.Printf("Y 继续,Q 退出\\n")
var tmep string
fmt.Scanln(&tmep)
if tmep == "Y" || tmep == "y" {
isContinue = true
}else if tmep == "Q" || tmep == "q" {
isContinue = false
}
}
}
第二版本
这个版本功能和第一版本是没有区别,仅仅是吧代码封装到几个函数里面。
/*
BMI = 体重(公斤) / (身高*身高)(米)
体制率: (1.2 * BMI + 0.23*年龄 - 5.4 -10.8*性别(男1女0)) / 100
*/
package main //声明包
import "fmt" //包含包
//全局变量声明
var(
weight float64 //体重(公斤)
height float64 //身高(米)
age int //年龄
sex string //性别
sexRate float64 //性别BMI转化值
bmi float64 //bmi值
fatRate float64 //体脂率
avearageFat float64 = 0.0 //平均体脂
peoplecount int = 0 //录入人数计数器
isContinue bool //循环采集是否继续标志
)
// Main 函数
func main() {
fmt.Println("BMI is Running…") //启动信息
for isContinue = true; isContinue; {
getInfor()
peoplecount ++
bmi = weight/(height * height)
fatRate = (1.2 * bmi + 0.23 * float64(age) - 5.4 - 10.8 * sexRate) /100
calculator()
fmt.Printf("+**************************************+\\n共录入%d人记录,平均体脂为%2.3f \\n" +
"+**************************************+\\n",
peoplecount,(avearageFat*float64(peoplecount-1)+fatRate)/float64(peoplecount))
exitbmi()
}
}
//获取用户输入信息
func getInfor() {
fmt.Printf("性别:")
fmt.Scanln(&sex)
fmt.Printf("体重(公斤):")
fmt.Scanln(&weight)
fmt.Printf("身高(米):")
fmt.Scanln(&height)
fmt.Printf("年龄:")
fmt.Scanln(&age)
if sex == "man" {
sexRate = 1
}else{
sexRate = 0
}
}
//计算用户输入信息
func calculator() {
fmt.Println("+**************************************+")
if sex == "man" {
if age >= 18 && age <=39 {
if fatRate <= 0.1 {
fmt.Printf("BMI:%2.3f\\n 体脂率:%2.3f\\n太瘦了\\n",bmi,fatRate)
}else if fatRate >0.1 &&fatRate <= 0.16 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n标准,太棒了\\n",bmi,fatRate)
}else if fatRate > 0.16 && fatRate <= 0.21 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n偏重,不影响把妹,加油\\n",bmi,fatRate)
}else if fatRate > 0.21 && fatRate <= 0.26 {
fmt.Printf("BMI:%2.3f\\n 体脂率:%2.3f\\n稍微有点胖了,需要控制\\n",bmi,fatRate)
}else {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n危险!危险!危险!要减肥了\\n",bmi,fatRate)
}
}else if age >= 40 && age <= 59{
if fatRate <= 0.11 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n太瘦了\\n",bmi,fatRate)
}else if fatRate >0.11 &&fatRate <= 0.17 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n标准,太棒了\\n",bmi,fatRate)
}else if fatRate > 0.17 && fatRate <= 0.22 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n偏重,不影响把妹,加油\\n",bmi,fatRate)
}else if fatRate > 0.22 && fatRate <= 0.27 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n稍微有点胖了,需要控制\\n",bmi,fatRate)
}else {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n危险!危险!危险!要减肥了\\n",bmi,fatRate)
}
}else{
if fatRate <= 0.13 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n太瘦了\\n",bmi,fatRate)
}else if fatRate >0.13 &&fatRate <= 0.19 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n标准,太棒了\\n",bmi,fatRate)
}else if fatRate > 0.19 && fatRate <= 0.24 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n偏重,不影响把妹,加油\\n",bmi,fatRate)
}else if fatRate > 0.24 && fatRate <= 0.29 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n稍微有点胖了,需要控制\\n",bmi,fatRate)
}else {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n危险!危险!危险!要减肥了\\n",bmi,fatRate)
}
}
}else{
if age >= 18 && age <=39 {
if fatRate <= 0.2 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n太瘦了\\n",bmi,fatRate)
}else if fatRate >0.2 &&fatRate <= 0.27 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n标准,太棒了\\n",bmi,fatRate)
}else if fatRate > 0.27 && fatRate <= 0.34 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n偏重,不影响,加油\\n",bmi,fatRate)
}else if fatRate > 0.34 && fatRate <= 0.39 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n稍微有点胖了,需要控制\\n",bmi,fatRate)
}else {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n危险!危险!危险!要减肥了\\n",bmi,fatRate)
}
}else if age >= 40 && age <= 59{
if fatRate <= 0.21 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n太瘦了\\n",bmi,fatRate)
}else if fatRate >0.21 &&fatRate <= 0.28 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n标准,太棒了\\n",bmi,fatRate)
}else if fatRate > 0.28 && fatRate <= 0.35 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n偏重,不影响,加油\\n",bmi,fatRate)
}else if fatRate > 0.35 && fatRate <= 0.40 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n稍微有点胖了,需要控制\\n",bmi,fatRate)
}else {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n危险!危险!危险!要减肥了\\n",bmi,fatRate)
}
}else{
if fatRate <= 0.22 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n太瘦了\\n",bmi,fatRate)
}else if fatRate >0.22&&fatRate <= 0.29 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n标准,太棒了\\n",bmi,fatRate)
}else if fatRate > 0.29&& fatRate <= 0.36 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n偏重,不影响,加油\\n",bmi,fatRate)
}else if fatRate > 0.36 && fatRate <= 0.41 {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n稍微有点胖了,需要控制\\n",bmi,fatRate)
}else {
fmt.Printf("BMI:%2.3f \\n体脂率:%2.3f\\n危险!危险!危险!要减肥了\\n",bmi,fatRate)
}
}
}
}
//退出继续操作
func exitbmi () {
fmt.Printf("Y 继续,Q 退出\\n")
var tmep string
fmt.Scanln(&tmep)
if tmep == "Y" || tmep == "y" {
isContinue = true
}else if tmep == "Q" || tmep == "q" {
isContinue = false
}
}