基于Java實現(xiàn)簡單的身材計算程序
更新時間:2022年12月21日 10:45:41 作者:南方者
這篇文章主要為大家詳細介紹了如何利用Java實現(xiàn)簡單的身材計算程序,可以計算身體的體脂率以及BMI數(shù)值等,感興趣的小伙伴可以跟隨小編一起學習一下
效果展示

完整代碼
代碼比較簡單,也有注釋,就不再詳細做介紹啦。
import java.util.Scanner;
public class Main extends Common {
public static void main(String[] args) {
// 身體健康計算情況
// 開始
start();
boolean flag = true;
Person person = new Person();
while (flag) {
showRule();
String inText = in();
while (inText == null || inText == "") {
show("輸入錯誤,請檢查是否有輸入。");
inText = in();
}
switch (inText) {
case "0":
show("歡迎下次光臨南方者小測!");
flag = false;
break;
case "1":
person.inName(); // 輸入名字(如:南方者)
person.inSex(); // 輸入性別
person.inAge(); // 輸入年齡
person.inHeight(); // 輸入身高
person.inWeight(); // 輸入體重
person.inBust(); // 輸入胸圍
person.inWaist(); // 輸入腰圍
person.inHip(); // 輸入臀圍
break;
case "2": // 顯示`BMI`計算公式
showBMI();
break;
case "3": // 計算`BMI`值
person.countBMI();
break;
case "4": // 顯示`體脂率值`計算公式
showBF();
break;
case "5": // 計算`體脂率值`
person.countBF();
break;
case "6":
person.display();
break;
default:
show("輸入錯誤,請檢查是否有輸入。");
}
}
}
public static void start() {
show("********************************");
show("計算身體健康情況 - by nanfangzhe");
show("---------------------------------");
show("說明:");
show("1. 輸入的參數(shù)值越多,計算標準越多。");
show("2. 根據(jù)提示進入下一步。");
show("(注:僅靠簡單數(shù)據(jù)計算出的結果,都有可能的誤差在8%以內(nèi);請以醫(yī)院醫(yī)生數(shù)據(jù)為標準。)");
show("---------------------------------");
}
/**
* 顯示規(guī)則
*/
public static void showRule() {
show("---------------------------------");
show("輸入提示:0 退出;1 輸入/修改參數(shù);2 顯示`BMI`計算公式;\n 3計算`BMI`值;4 顯示`體脂率值`計算公式;5 計算`體脂率值`;6 輸出個人情況;");
show("---------------------------------");
}
/**
* 體脂率情況
*/
public static void showBF() {
show("********************************");
show("計算體脂率:1.2×BMI+0.23×年齡-5.4-10.8×性別(男1,女0)");
show("---------------------------------");
show("丨 類型 丨 體脂率范圍");
show("丨 男 丨 15%~18%");
show("丨 女 丨 20%~30%");
show("---------------------------------");
}
/**
* 三圍
*/
public static void show3Point() {
show("********************************");
show("標準三圍計算");
show("-----------------------------------");
show("丨 類型 丨 男 丨 女 ");
show("丨 胸圍 丨 身高×0.61 丨 身高×0.61×0.9 ");
show("丨 腰圍 丨 身高×0.42 丨 身高×0.42×0.89 ");
show("丨 臀圍 丨 身高×0.64 丨 身高×0.64×1.02 ");
show("-----------------------------------");
}
public static void showBMI() {
show("********************************");
show("BMI 中國標準(計算公式:BMI = 體重(kg)/ 身高的平方(m)");
show("---------------------------------");
show("丨 分類 丨 BMI 范圍 ");
show("丨 偏瘦 丨 <= 18.4 ");
show("丨 正常 丨 18.5 ~ 23.9 ");
show("丨 過重 丨 24.0 ~ 27.9 ");
show("丨 肥胖 丨 >= 28.0 ");
show("---------------------------------");
}
}
/**
* 對應用戶的類
*/
class Person extends Common {
static String notInputText = "(待輸入)";
String name = "你"; // 姓名 默認稱呼 “你”
int age; // 年齡
int sex = -1; // 性別(1男 0女)
float height; // 體重
float weight; // 身高
float bmi; // BMI值
float bust; // 胸圍
float waist; // 腰圍
float hip; // 臀圍
float canonBust; // 標準胸圍
float canonWaist; // 標準腰圍
float canonHip; // 標準臀圍
float bf; // 體脂率
String text = "暫無";
public void inName() {
show("輸入姓名");
this.name = in();
}
public void inAge() {
show("輸入年齡");
String str = in();
while (!isNumber(str)) {
show("輸入格式錯誤,請檢查。(格式:整數(shù);如:18歲,輸入18(多余小數(shù)會自動去掉)");
str = in();
}
try {
float f = Float.parseFloat(str);
this.age = (int) f;
} catch (Exception e) {
show("輸入錯誤,請重試。");
this.inAge();
}
}
public void inSex() {
show("輸入性別(男1 女0 其他為跳過)");
String str = in();
while (!isNumber(str)) {
show("輸入格式錯誤,請檢查。(格式:整數(shù);男1 女0");
str = in();
}
try {
float f = Float.parseFloat(str);
if (f == 0 || f == 1) {
this.sex = (int) f;
}
} catch (Exception e) {
show("輸入錯誤,請重試。");
this.inSex();
}
}
public void inHeight() {
show("輸入身高(輸入0為跳過)");
String str = in();
while (!isNumber(str)) {
show("輸入格式錯誤,請檢查。(格式為:數(shù)字;例如身高184cm,輸入184)");
str = in();
}
try {
if (isZero(str)) {
// 0為跳過
return;
}
this.height = Float.parseFloat(str);
} catch (Exception e) {
show("輸入錯誤,請重試。");
this.inHeight();
}
}
public void inWeight() {
show("輸入體重(輸入0為跳過)");
String str = in();
while (!isNumber(str)) {
show("輸入格式錯誤,請檢查。(格式為:數(shù)字;例如體重90.2kg,輸入90.2)");
str = in();
}
try {
if (isZero(str)) {
// 0為跳過
return;
}
this.weight = Float.parseFloat(str);
} catch (Exception e) {
show("輸入錯誤,請重試。");
this.inWeight();
}
}
public void inBust() {
show("輸入胸圍(輸入0為跳過)");
String str = in();
while (!isNumber(str)) {
show("輸入格式錯誤,請檢查。(格式為:數(shù)字;例如胸圍37.5,輸入37.5)");
str = in();
}
try {
if (isZero(str)) {
return;
}
this.bust = Float.parseFloat(str);
} catch (Exception e) {
show("輸入錯誤,請重試。");
this.inBust();
}
}
public void inWaist() {
show("輸入腰圍(輸入0為跳過)");
String str = in();
while (!isNumber(str)) {
show("輸入格式錯誤,請檢查。(格式為:數(shù)字;例如腰圍50.5,輸入50.5)");
str = in();
}
try {
if (isZero(str)) {
return;
}
this.waist = Float.parseFloat(str);
} catch (Exception e) {
this.inWaist();
show("輸入錯誤,請重試。");
}
}
public void inHip() {
show("輸入臀圍(輸入0為跳過)");
String str = in();
while (!isNumber(str)) {
show("輸入格式錯誤,請檢查。(格式為:數(shù)字;例如臀圍50.5,輸入50.5)");
str = in();
}
try {
if (isZero(str)) {
return;
}
this.hip = Float.parseFloat(str);
} catch (Exception e) {
this.inHip();
show("輸入錯誤,請重試。");
}
}
/**
* 計算 BMI
*/
public void countBMI() {
if (this.weight == 0 || this.weight < 1 || this.height < 1) {
show("請檢查當前的身高和體重是否有值。(計算 BMI 失?。?);
return;
}
this.bmi = this.weight / (this.height * this.height) * 100 * 100; // 身高是以米為單位(因此,乘上兩個100)
show("計算成功!你的BMI值為:" + this.bmi);
}
/**
* 計算標準三圍
*/
public void count3Point() {
if (this.height == 0 || this.height < 1 || (this.sex != 0 && this.sex != 1)) {
show("請檢查當前的身高和性別是否有值。(計算標準三圍失?。?);
return;
}
this.canonBust = this.height * 0.61f;
this.canonWaist = this.height * 0.42f;
this.canonHip = this.height * 0.64f;
// 如果是女性,需要乘上對應的參數(shù)
if (this.sex == 0) {
this.canonBust *= 0.9f;
this.canonWaist *= 0.89f;
this.canonHip *= 1.02f;
}
}
/**
* 計算體脂率
*/
public void countBF() {
if (this.bmi == 0 || this.age == 0 || (this.sex != 0 && this.sex != 1)) {
show("請檢查當前的身高、體重、年齡、性別是否有值。(計算 體脂率 失敗!");
return;
}
this.bf = this.bmi * 1.2f + 0.23f * this.age - 5.4f - 10.8f * this.sex;
show("計算成功!你的體脂率值為:" + this.bf);
}
public void display() {
// countBMI();
// count3Point();
// countBF();
// 輸出身體情況
String ageStr = "" + (this.age == 0 ? notInputText : this.age);
String sexStr = this.sex == -1 ? notInputText : (this.sex == 1 ? "男" : "女");
String heightStr = this.height == 0 ? notInputText : (this.height + "cm");
String weightStr = this.weight == 0 ? notInputText : (this.weight + "kg");
String bmiStr = this.bmi == 0 ? notInputText : this.bmi + "";
String bfStr = this.bf == 0 ? notInputText : this.bf + "";
show(this.name + "的身體情況");
show("---------------------------------");
show("丨 類型 丨 值");
show("---------------------------------");
show("丨 年齡 丨 " + ageStr);
show("丨 性別 丨 " + sexStr);
show("丨 身高 丨 " + heightStr);
show("丨 體重 丨 " + weightStr);
show("丨 BMI 丨 " + bmiStr);
show("丨 體脂率 丨 " + bfStr);
show("丨 最終建議: " + this.text);
show("---------------------------------");
}
public boolean isNumber(String str) {
String regex = "\d+(.\d+)?";
return str.matches(regex);
}
public static boolean isZero(String str) {
return Float.parseFloat(str) == 0;
}
}
class Common {
/**
* 僅為了方便輸出
*
* @param str
*/
public static void show(String str) {
System.out.println(str);
}
/**
* 輸入
*
* @return
*/
public static String in() {
Scanner scanner = new Scanner(System.in);
String next = scanner.next();
while (next == null || next.isEmpty() || next.trim().isEmpty()) {
show("輸入有誤,請重新輸入。");
next = scanner.next();
}
return next;
}
}到此這篇關于基于Java實現(xiàn)簡單的身材計算程序的文章就介紹到這了,更多相關Java身材計算內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring項目中使用Cache?Redis實現(xiàn)數(shù)據(jù)緩存
這篇文章主要為大家介紹了項目中使用Spring?Cache?Redis實現(xiàn)數(shù)據(jù)緩存,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06
IDEA設置maven修改settings.xml配置文件無法加載倉庫的解決方案
這篇文章主要介紹了IDEA設置maven修改settings.xml配置文件無法加載倉庫的解決方案,幫助大家更好的利用IDEA進行JAVA的開發(fā)學習,感興趣的朋友可以了解下2021-01-01
SpringBoot實現(xiàn)賬號登錄錯誤次數(shù)的限制和鎖定功能
本文介紹了如何使用SpringBoot和Redis實現(xiàn)賬號登錄錯誤次數(shù)限制和鎖定功能,通過自定義注解和AOP切面,結合配置文件靈活設置最大嘗試次數(shù)和鎖定時長,感興趣的朋友跟隨小編一起看看吧2024-12-12
Spring-Cloud Eureka注冊中心實現(xiàn)高可用搭建
這篇文章主要介紹了Spring-Cloud Eureka注冊中心實現(xiàn)高可用搭建,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
Maven介紹與配置+IDEA集成Maven+使用Maven命令小結
Maven是Apache軟件基金會的一個開源項目,是一個優(yōu)秀的項目構建管理工具,它用來幫助開發(fā)者管理項目中的 jar,以及 jar 之間的依賴關系、完成項目的編譯、測試、打包和發(fā)布等工作,本文給大家介紹Maven介紹與配置+IDEA集成Maven+使用Maven命令,感興趣的朋友一起看看吧2024-01-01

