-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
165 lines (157 loc) · 5.36 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import "fmt"
func main() {
//先定义三人的姓名、性别、身高、体重、年龄信息
var name [3]string
var sex [3]string
var tall [3]float64
var weight [3]float64
var age [3]int
//为三人的各项信息赋值
for i := 0; i < 3; i++ {
fmt.Printf("请输入第%d个人的姓名:", i+1)
_, err := fmt.Scanln(&name[i])
if err != nil {
fmt.Print("请输入正确的信息")
}
fmt.Printf("请输入第%d个人的性别:", i+1)
_, err = fmt.Scanln(&sex[i])
if err != nil {
fmt.Print("请输入正确的信息")
}
fmt.Printf("请输入第%d个人的身高:", i+1)
_, err = fmt.Scanln(&tall[i])
if err != nil {
fmt.Print("请输入正确的信息")
}
fmt.Printf("请输入第%d个人的体重:", i+1)
_, err = fmt.Scanln(&weight[i])
if err != nil {
fmt.Print("请输入正确的信息")
}
fmt.Printf("请输入第%d个人的年龄:", i+1)
_, err = fmt.Scanln(&age[i])
if err != nil {
fmt.Print("请输入正确的信息")
}
}
fmt.Println()
//根据三人的性别确定性别权重
var sexWeight [3]int
for i := 0; i < 3; i++ {
if sex[i] == "男" {
sexWeight[i] = 1
} else if sex[i] == "女" {
sexWeight[i] = 0
} else {
fmt.Println("性别错误,请输入正确的性别")
continue
}
}
//分别计算三人的BMI、体脂率
var bmi [3]float64
var fatRate [3]float64
for i := 0; i < 3; i++ {
bmi[i] = weight[i] / (tall[i] * tall[i])
fatRate[i] = (1.2*bmi[i] + 0.23*float64(age[i]) - 5.4 - 10.8*float64(sexWeight[i])) / 100
}
//分别为三人匹配体脂率建议
var suggest [3]string
for i := 0; i < 3; i++ {
if sex[i] == "男" {
//编写男性体脂状态表
if age[i] >= 18 && age[i] <= 39 {
if fatRate[i] <= 0.1 {
suggest[i] = "偏瘦,多吃多锻炼,增强体质"
} else if fatRate[i] > 0.1 && fatRate[i] <= 0.16 {
suggest[i] = "标准,太棒了,要保持哦"
} else if fatRate[i] > 0.16 && fatRate[i] <= 0.21 {
suggest[i] = "偏胖,少吃晚饭多散步,加油哦"
} else if fatRate[i] > 0.21 && fatRate[i] <= 0.26 {
suggest[i] = "肥胖,少吃多运动"
} else {
suggest[i] = "非常肥胖,建议医院检查"
}
} else if age[i] >= 40 && age[i] <= 59 {
if fatRate[i] <= 0.11 {
suggest[i] = "偏瘦,多吃多锻炼,增强体质"
} else if fatRate[i] > 0.12 && fatRate[i] <= 0.17 {
suggest[i] = "标准,太棒了,要保持哦"
} else if fatRate[i] > 0.18 && fatRate[i] <= 0.22 {
suggest[i] = "偏胖,少吃晚饭多散步,加油哦"
} else if fatRate[i] > 0.22 && fatRate[i] <= 0.27 {
suggest[i] = "肥胖,少吃多运动"
} else {
suggest[i] = "非常肥胖,建议医院检查"
}
} else if age[i] >= 60 {
if fatRate[i] <= 0.13 {
suggest[i] = "偏瘦,多吃多锻炼,增强体质"
} else if fatRate[i] > 0.13 && fatRate[i] <= 0.19 {
suggest[i] = "标准,太棒了,要保持哦"
} else if fatRate[i] > 0.19 && fatRate[i] <= 0.24 {
suggest[i] = "偏胖,少吃晚饭多散步,加油哦"
} else if fatRate[i] > 0.24 && fatRate[i] <= 0.29 {
suggest[i] = "肥胖,少吃多运动"
} else {
suggest[i] = "非常肥胖,建议医院检查"
}
} else {
suggest[i] = "抱歉,未成年人体脂率无法计算"
}
} else if sex[i] == "女" {
if age[i] >= 18 && age[i] <= 39 {
if fatRate[i] <= 0.2 {
suggest[i] = "偏瘦,多吃多锻炼,增强体质"
} else if fatRate[i] > 0.2 && fatRate[i] <= 0.27 {
suggest[i] = "标准,太棒了,要保持哦"
} else if fatRate[i] > 0.27 && fatRate[i] <= 0.34 {
suggest[i] = "偏胖,少吃晚饭多散步,加油哦"
} else if fatRate[i] > 0.34 && fatRate[i] <= 0.39 {
suggest[i] = "肥胖,少吃多运动"
} else {
suggest[i] = "非常肥胖,建议医院检查"
}
} else if age[i] >= 40 && age[i] <= 59 {
if fatRate[i] <= 0.21 {
suggest[i] = "偏瘦,多吃多锻炼,增强体质"
} else if fatRate[i] > 0.21 && fatRate[i] <= 0.28 {
suggest[i] = "标准,太棒了,要保持哦"
} else if fatRate[i] > 0.28 && fatRate[i] <= 0.35 {
suggest[i] = "偏胖,少吃晚饭多散步,加油哦"
} else if fatRate[i] > 0.35 && fatRate[i] <= 0.4 {
suggest[i] = "肥胖,少吃多运动"
} else {
suggest[i] = "非常肥胖,建议医院检查"
}
} else if age[i] >= 60 {
if fatRate[i] <= 0.22 {
suggest[i] = "偏瘦,多吃多锻炼,增强体质"
} else if fatRate[i] > 0.22 && fatRate[i] <= 0.29 {
suggest[i] = "标准,太棒了,要保持哦"
} else if fatRate[i] > 0.29 && fatRate[i] <= 0.36 {
suggest[i] = "偏胖,少吃晚饭多散步,加油哦"
} else if fatRate[i] > 0.36 && fatRate[i] <= 0.41 {
suggest[i] = "肥胖,少吃多运动"
} else {
suggest[i] = "非常肥胖,建议医院检查"
}
} else {
suggest[i] = "抱歉,未成年人体脂率无法计算"
}
}
}
//输出三人的姓名、BMI、体脂率、建议
for i := 0; i < 3; i++ {
fmt.Printf("%s 的 bmi 为 %.2f 体脂率为 %.2f 【%s】\n", name[i], bmi[i], fatRate[i], suggest[i])
}
fmt.Println()
//输出总人数、平均体脂率
var sum int
var fatRateSum float64
for i, v := range fatRate {
fatRateSum += v
sum += i
}
fmt.Printf("总人数为 %d,平均体脂率为 %f", sum, fatRateSum/float64(sum))
}