- public class BMIManager {
- float mHeight;
- float mWeight;
- String mSex;
- float mBMI;
- 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(float h , float w)
- {
- mBMI = (w/((h/100)*(h/100)));
- return mBMI;
- }
-
- String suggestion()
- {
- if (mBMI<18.5)
- {
- msuggestion="體重過輕喔!!";
- }
- else if(mBMI>=18.5 && mBMI<24)
- {
- msuggestion="恭喜,正常範圍喔!!";
- }
- else if(mBMI>=24 && mBMI<27)
- {
- msuggestion="過重!!";
- }
- else if(mBMI>=27 && mBMI<30)
- {
- msuggestion="輕度肥胖!!";
- }
- else if(mBMI>=30 && mBMI<35)
- {
- msuggestion="中度肥胖!!";
- }
- else if(mBMI>=35)
- {
- msuggestion="重度肥胖!!";
- }
- else
- {
- msuggestion="輸入錯誤喔!!";
- }
- return msuggestion;
- }
-
- public static void main(String[] args) {
-
-
-
-
-
- }
- }
- 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(hight, weight));
- System.out.println(myBMI.suggestion());
-
-
-
- }
- }
- 請輸入您的姓名=knight
- 請輸入你的身高(cm)=170
- 請輸入你的體重(kg)=66
- 請輸入你的性別 M or F =M
- 親愛的 knight 男士 ,您的BMI=22.83737
- 恭喜,正常範圍喔!!
複製代碼 |