Files
purpleschool/go-demo/main.go
Archie Fox d1eea4bb66 Init commit
2025-02-24 15:49:59 +03:00

70 lines
1.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"errors"
"fmt"
"math"
)
func main() {
fmt.Println("__ Калькулятор индекса массы тела __")
for {
userKg, userHeight := getUserInput()
IMT, err := calculateIMT(userKg, userHeight)
if err != nil {
// fmt.Println(err)
// continue
panic("Не заданы параметры для расчета")
}
outputResult(IMT)
fmt.Print("Хотите расчитать еще (Y/n): ")
var choice string
fmt.Scan(&choice)
if choice == "n" {
break
}
if choice == "Y" || choice == "y" {
continue
}
}
}
func outputResult(imt float64) {
result := fmt.Sprintf("Ваш индекс массы тела: %.1f\n", imt)
fmt.Print(result)
switch {
case imt < 16:
fmt.Println("У вас сильный дефицит массы тела")
case imt < 18.5:
fmt.Println("У вас дефицит массы тела")
case imt < 25:
fmt.Println("У вас нормальный вес")
case imt < 30:
fmt.Println("У вас избыточный вес")
default:
fmt.Println("У вас степень ожирения")
}
}
func calculateIMT(userKg float64, userHeight float64) (float64, error) {
const IMTPower float64 = 2
if userHeight <= 0 || userKg <= 0 {
return 0, errors.New("NO_VALID_PARAMS")
}
IMT := userKg / math.Pow(userHeight/100, IMTPower)
return IMT, nil
}
func getUserInput() (float64, float64) {
var userHeight float64
var userKg float64
fmt.Print("Введите свой рост в сантиметрах: ")
fmt.Scan(&userHeight)
fmt.Print("Введите свой вес в килограммах: ")
fmt.Scan(&userKg)
return userKg, userHeight
}