基于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("說明:"); 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 中國標(biāo)準(zhǔn)(計(jì)算公式:BMI = 體重(kg)/ 身高的平方(m)"); show("---------------------------------"); show("丨 分類 丨 BMI 范圍 "); show("丨 偏瘦 丨 <= 18.4 "); show("丨 正常 丨 18.5 ~ 23.9 "); show("丨 過重 丨 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 = "暫無"; 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 其他為跳過)"); 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為跳過)"); String str = in(); while (!isNumber(str)) { show("輸入格式錯(cuò)誤,請(qǐng)檢查。(格式為:數(shù)字;例如身高184cm,輸入184)"); str = in(); } try { if (isZero(str)) { // 0為跳過 return; } this.height = Float.parseFloat(str); } catch (Exception e) { show("輸入錯(cuò)誤,請(qǐng)重試。"); this.inHeight(); } } public void inWeight() { show("輸入體重(輸入0為跳過)"); String str = in(); while (!isNumber(str)) { show("輸入格式錯(cuò)誤,請(qǐng)檢查。(格式為:數(shù)字;例如體重90.2kg,輸入90.2)"); str = in(); } try { if (isZero(str)) { // 0為跳過 return; } this.weight = Float.parseFloat(str); } catch (Exception e) { show("輸入錯(cuò)誤,請(qǐng)重試。"); this.inWeight(); } } public void inBust() { show("輸入胸圍(輸入0為跳過)"); 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為跳過)"); 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為跳過)"); 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)文章
mybatis如何獲取剛剛新插入數(shù)據(jù)的主鍵值id
這篇文章主要介紹了mybatis如何獲取剛剛新插入數(shù)據(jù)的主鍵值id問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08Spring Boot集成tablesaw插件快速入門示例代碼
Tablesaw是一款Java的數(shù)據(jù)可視化庫,數(shù)據(jù)解析庫,主要用于加載數(shù)據(jù),對(duì)數(shù)據(jù)進(jìn)行操作(轉(zhuǎn)化,過濾,匯總等),類比Python中的Pandas庫,本文介紹Spring Boot集成tablesaw插件快速入門Demo,感興趣的朋友一起看看吧2024-06-06全網(wǎng)最精細(xì)詳解二叉樹,2萬字帶你進(jìn)入算法領(lǐng)域
大家好,我是哪吒,一個(gè)熱愛編碼的Java工程師,本著"欲速則不達(dá),欲達(dá)則欲速"的學(xué)習(xí)態(tài)度,在程序猿這條不歸路上不斷成長(zhǎng),所謂成長(zhǎng),不過是用時(shí)間慢慢擦亮你的眼睛,少時(shí)看重的,年長(zhǎng)后卻視若鴻毛,少時(shí)看輕的,年長(zhǎng)后卻視若泰山,成長(zhǎng)之路,亦是漸漸放下執(zhí)念,內(nèi)心歸于平靜的旅程2021-08-08JAVA8獲取list集合中重復(fù)的元素與獲取去重?cái)?shù)據(jù)實(shí)例
這篇文章主要給大家介紹了關(guān)于JAVA8獲取list集合中重復(fù)的元素與獲取去重?cái)?shù)據(jù)的相關(guān)資料,在實(shí)際開發(fā)中經(jīng)常會(huì)遇到需要找出(刪除)一個(gè)list中某些元素的屬性相同的元素,需要的朋友可以參考下2023-07-07Java操作mongodb增刪改查的基本操作實(shí)戰(zhàn)指南
MongoDB是一個(gè)基于分布式文件存儲(chǔ)的數(shù)據(jù)庫,由c++語言編寫,旨在為WEB應(yīng)用提供可擴(kuò)展的高性能數(shù)據(jù)存儲(chǔ)解決方案,下面這篇文章主要給大家介紹了關(guān)于Java操作mongodb增刪改查的基本操作實(shí)戰(zhàn)指南,需要的朋友可以參考下2023-05-05解析Flink內(nèi)核原理與實(shí)現(xiàn)核心抽象
Flink API提供了開發(fā)的接口,此外,為了實(shí)現(xiàn)業(yè)務(wù)邏輯,還必須為開發(fā)者提供自定義業(yè)務(wù)邏輯的能力,下面為大家解析Flink內(nèi)核原理與實(shí)現(xiàn)核心抽象2021-08-08Java實(shí)現(xiàn)從Html文本中提取純文本的方法
今天小編就為大家分享一篇Java實(shí)現(xiàn)從Html文本中提取純文本的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05