- import java.util.Scanner;
- class ManSuggestion
- {
- String suggestion(float mBMI)
- {
- String mSuggestion = "";
- if(mBMI >= 35)
- {
- mSuggestion = "重度肥胖,你是神豬嗎 ?";
- }
- else if(mBMI >= 30 && mBMI <35)
- {
- mSuggestion = "中度肥胖,恭喜你可以請人家幫忙綁鞋帶!";
- }
- else if(mBMI >= 27 && mBMI <30)
- {
- mSuggestion = "過重,低頭快看不到你的腳趾頭了!";
- }
- else if(mBMI >= 24 && mBMI <27)
- {
- mSuggestion = "稍重,恭喜你還可以蹲下去綁鞋帶!";
- }
- else if(mBMI >= 18.5 && mBMI <24)
- {
- mSuggestion = "標準,帥哥一枚保持下去!";
- }
- else if(mBMI < 18.5)
- {
- mSuggestion = "過輕!!,紙片人!";
- }
- return mSuggestion;
- }
- }
- class WomanSuggestion
- {
- String suggestion(float mBMI)
- {
- String mSuggestion = "";
- if(mBMI >= 35)
- {
- mSuggestion = "不要每年都咬橘子壓!";
- }
- else if(mBMI >= 30 && mBMI <35)
- {
- mSuggestion = "太豐腴了 , 擋住我的電視了!";
- }
- else if(mBMI >= 27 && mBMI <30)
- {
- mSuggestion = "有點豐腴喔 , 注意一下飲食喔!";
- }
- else if(mBMI >= 24 && mBMI <27)
- {
- mSuggestion = "體重有點重,少吃一點會更好喔!";
- }
- else if(mBMI >= 18.5 && mBMI <24)
- {
- mSuggestion = "標準喔,正妹一個喔!";
- }
- else if(mBMI < 18.5)
- {
- mSuggestion = "過輕,再多吃一點會更好喔!";
- }
- return mSuggestion;
- }
- }
- public class BMIManager {
- private float mHeight;
- private float mWeight;
- private String mSex;
- private float mBMI=0;
- String msuggestion;
-
- void setHeight(float h)
- {
- this.mHeight = h;
- }
-
- float getHeight()
- {
- return this.mHeight;
- }
-
- void setWeight(float w)
- {
- this.mWeight = w;
- }
-
- float getWeight()
- {
- return this.mWeight;
- }
- void setSex(String s)
- {
- this.mSex=s;
- }
-
- String getSex()
- {
-
- if (mSex.equals("M"))
- mSex="男士";
- else if(mSex.equals("F"))
- mSex="女士";
- else
- mSex="無法判別";
-
- return this.mSex;
- }
-
-
-
- BMIManager(float h , float w , String s)
- {
- this.mHeight=h;
- this.mWeight=w;
- this.mSex=s;
- }
-
- float calculateBMI()
- {
- if (mBMI==0)
- {
- mBMI = (mWeight/((mHeight/100)*(mHeight/100)));
- }
- return mBMI;
- }
-
- String suggestion()
- {
- if(mBMI == 0)
- calculateBMI();
-
- if (mSex.equals("男士"))
- return (new ManSuggestion()).suggestion(mBMI);
- else
- return (new WomanSuggestion()).suggestion(mBMI);
- }
-
-
-
- }
- import java.util.Scanner;
- public class BMIManagerTest {
-
- public static void main(String[] args) {
-
-
- String name,sex;
- float hight;
- float weight;
-
- Scanner scan = new Scanner(System.in);
- System.out.print("請輸入您的姓名=");
- name=scan.next();
- System.out.print("請輸入你的身高(cm)=");
- hight=Float.parseFloat(scan.next());
- System.out.print("請輸入你的體重(kg)=");
- weight=Float.parseFloat(scan.next());
-
- System.out.print("請輸入你的性別 M or F =");
- sex=scan.next();
- BMIManager myBMI = new BMIManager (hight , weight , sex);
-
- System.out.println("親愛的 "+name+" "+myBMI.getSex()+" ,您的BMI="+myBMI.calculateBMI());
- System.out.println(myBMI.suggestion());
-
-
- }
- }
- 請輸入您的姓名=kkman
- 請輸入你的身高(cm)=170
- 請輸入你的體重(kg)=99
- 請輸入你的性別 M or F =M
- 親愛的 kkman 男士 ,您的BMI=34.256054
- 中度肥胖,恭喜你可以請人家幫忙綁鞋帶!
複製代碼 |