基于Java實(shí)現(xiàn)簡(jiǎn)單的身材計(jì)算程序
效果展示

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

