基于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ù)計算出的結(jié)果,都有可能的誤差在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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
mybatis如何獲取剛剛新插入數(shù)據(jù)的主鍵值id
這篇文章主要介紹了mybatis如何獲取剛剛新插入數(shù)據(jù)的主鍵值id問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08Spring Boot集成tablesaw插件快速入門示例代碼
Tablesaw是一款Java的數(shù)據(jù)可視化庫,數(shù)據(jù)解析庫,主要用于加載數(shù)據(jù),對數(shù)據(jù)進行操作(轉(zhuǎn)化,過濾,匯總等),類比Python中的Pandas庫,本文介紹Spring Boot集成tablesaw插件快速入門Demo,感興趣的朋友一起看看吧2024-06-06JAVA8獲取list集合中重復的元素與獲取去重數(shù)據(jù)實例
這篇文章主要給大家介紹了關于JAVA8獲取list集合中重復的元素與獲取去重數(shù)據(jù)的相關資料,在實際開發(fā)中經(jīng)常會遇到需要找出(刪除)一個list中某些元素的屬性相同的元素,需要的朋友可以參考下2023-07-07Java操作mongodb增刪改查的基本操作實戰(zhàn)指南
MongoDB是一個基于分布式文件存儲的數(shù)據(jù)庫,由c++語言編寫,旨在為WEB應用提供可擴展的高性能數(shù)據(jù)存儲解決方案,下面這篇文章主要給大家介紹了關于Java操作mongodb增刪改查的基本操作實戰(zhàn)指南,需要的朋友可以參考下2023-05-05