返回列表 發帖
本帖最後由 吳尚哲 於 2012-10-14 21:12 編輯
  1. public class BMIManager
  2. {
  3.         String mName;
  4.         float mHeight;
  5.         float mWeight;
  6.         boolean mSex;
  7.         float mBMI;
  8.         String mSuggestion;
  9.        
  10.         BMIManager(String n, float h, float w, boolean s)                //建構子
  11.         {
  12.                 this.mName = n;
  13.                 this.mHeight = h;
  14.                 this.mWeight = w;
  15.                 this.mSex = s;
  16.         }
  17.        
  18.         void setName(String n)
  19.         {
  20.                 this.mName = n;
  21.         }
  22.        
  23.         void setHeight(float h)
  24.         {
  25.                 this.mHeight = h;
  26.         }
  27.        
  28.         void setWeight(float w)
  29.         {
  30.                 this.mHeight = w;
  31.         }
  32.        
  33.         void setSex(boolean s)
  34.         {
  35.                 this.mSex = s;
  36.         }
  37.        
  38.         float getHeight()
  39.         {
  40.                 return this.mHeight;
  41.         }
  42.        
  43.         float getWeight()
  44.         {
  45.                 return this.mWeight;
  46.         }
  47.        
  48.         boolean getSex()
  49.         {
  50.                 return this.mSex;
  51.         }
  52.        
  53.         float caculateBMI()
  54.         {
  55.                 this.mBMI = this.mWeight/((this.mHeight/100)*(this.mHeight/100));
  56.                 return this.mBMI;
  57.         }
  58.        
  59.         String suggestion()
  60.     {
  61.                 if(this.mBMI >= 35)
  62.                 {       
  63.                         this.mSuggestion = this.mName + "重度肥胖";
  64.                 }
  65.                
  66.                 else if(this.mBMI >= 30 && this.mBMI < 35)
  67.                 {
  68.                         this.mSuggestion = this.mName + (mSex?"先生":"女士") +"中度肥胖 BMI:" + this.mBMI;
  69.                 }
  70.                
  71.                 else if(this.mBMI >= 27 && this.mBMI < 30)
  72.                 {
  73.                         this.mSuggestion = this.mName + (mSex?"先生":"女士")  + "輕度肥胖 BMI:" + this.mBMI;
  74.                 }
  75.                
  76.                 else if(this.mBMI >= 24 && this.mBMI < 27)
  77.                 {
  78.                         this.mSuggestion = this.mName + (mSex?"先生":"女士")  + "過重 BMI:" + this.mBMI;
  79.                 }
  80.                
  81.                 else if(this.mBMI >= 18.5 && this.mBMI < 24)
  82.                 {
  83.                         this.mSuggestion = this.mName + (mSex?"先生":"女士")  + "標準 BMI:" + this.mBMI;
  84.                 }
  85.                
  86.                 else if(this.mBMI < 18.5)
  87.                 {
  88.                         this.mSuggestion = this.mName + (mSex?"先生":"女士")  + "過輕 BMI:" + this.mBMI;
  89.                 }
  90.                
  91.                 else
  92.                 {
  93.                         this.mSuggestion = "你確定??" + this.mBMI;
  94.                 }
  95.                
  96.                 return this.mSuggestion;
  97.     }

  98. }
複製代碼
  1. import java.util.Scanner;


  2. public class MyBMI {
  3.         public static void main(String[] args)
  4.         {
  5.                 Scanner scan = new Scanner(System.in);
  6.                 String name;
  7.                 float h;
  8.                 float w;
  9.                 boolean s;
  10.                 System.out.println("請輸入您的大名 : ");
  11.                 name = scan.next();
  12.                 System.out.println("請輸入您的身高(公分) : ");
  13.                 h = Float.parseFloat(scan.next());
  14.                 System.out.println("請輸入您的體重(公斤) : ");
  15.                 w = Float.parseFloat(scan.next());
  16.                 System.out.println("請輸入您的性別男1/女0 : ");
  17.                 s = Integer.parseInt(scan.next())==1;
  18.                
  19.                 BMIManager myBMI = new BMIManager(name, h, w, s);
  20.                
  21.                 myBMI.caculateBMI();
  22.                 System.out.println(myBMI.suggestion());
  23.         }

  24. }
複製代碼
  1. 請輸入您的大名 :
  2. 阿哲
  3. 請輸入您的身高(公分) :
  4. 170
  5. 請輸入您的體重(公斤) :
  6. 78
  7. 請輸入您的性別男1/女0 :
  8. 1
  9. 阿哲先生過重 BMI:26.989618
複製代碼
老師我想請教一下
建構子的功能是在物件實體化的過程中將內容初始化而已嗎?
除此之外還有其他用意嗎?
和用set方法給予初始值有什麼不一樣?

TOP

返回列表