返回列表 發帖
  1. class ManSuggestion
  2. {
  3.                
  4.         String mSuggestion;
  5.         String mSuggestion(float mBMI, String mName)
  6.         {       
  7.                 if(mBMI >= 35)
  8.                 {       
  9.                         this.mSuggestion = mName + " 先生" + " 你胖死啦!! BMI: " + mBMI;
  10.                 }
  11.        
  12.                 else if(mBMI >= 30 && mBMI < 35)
  13.                 {
  14.                         this.mSuggestion = mName + " 先生" + " 你胖過頭囉! BMI:" + mBMI;
  15.                 }
  16.                
  17.                 else if(mBMI >= 27 && mBMI < 30)
  18.                 {
  19.                         this.mSuggestion = mName + " 先生" + " 你太胖囉 ~BMI:" + mBMI;
  20.                 }
  21.                
  22.                 else if(mBMI >= 24 && mBMI < 27)
  23.                 {
  24.                         this.mSuggestion = mName + " 先生"  + " 你吃太多了唷 BMI:" + mBMI;
  25.                 }
  26.                
  27.                 else if(mBMI >= 18.5 && mBMI < 24)
  28.                 {
  29.                         this.mSuggestion = mName + " 先生"  + " 真的這麼標準嗎=.= BMI:" + mBMI;
  30.                 }
  31.                
  32.                 else if(mBMI < 18.5)
  33.                 {
  34.                         this.mSuggestion = mName + " 先生"  + " 你是沒在吃飯嗎?? BMI:" + mBMI;
  35.                 }
  36.                
  37.                 else
  38.                 {
  39.                         this.mSuggestion = "你確定??" + mBMI;
  40.                 }
  41.                
  42.                 return mSuggestion;
  43.                        
  44.         }

  45. }


  46. class WomanSuggestion
  47. {
  48.         String mSuggestion;
  49.         String mSuggestion(float mBMI, String mName)
  50.         {       
  51.                 if(mBMI >= 35)
  52.                 {       
  53.                         this.mSuggestion = mName + " 女士" + " 請注意健康! BMI: " + mBMI;
  54.                 }
  55.        
  56.                 else if(mBMI >= 30 && mBMI < 35)
  57.                 {
  58.                         this.mSuggestion = mName + " 女士" + " 該控制一下飲食作息囉 ... BMI:" + mBMI;
  59.                 }
  60.                
  61.                 else if(mBMI >= 27 && mBMI < 30)
  62.                 {
  63.                         this.mSuggestion = mName + " 女士" + " 瘦一點會更好唷~ BMI:" + mBMI;
  64.                 }
  65.                
  66.                 else if(mBMI >= 24 && mBMI < 27)
  67.                 {
  68.                         this.mSuggestion = mName + " 女士"  + " 還算標準啦^^ BMI:" + mBMI;
  69.                 }
  70.                
  71.                 else if(mBMI >= 18.5 && mBMI < 24)
  72.                 {
  73.                         this.mSuggestion = mName + " 女士"  + " 妳十分健康唷^^ BMI:" + mBMI;
  74.                 }
  75.                
  76.                 else if(mBMI < 18.5)
  77.                 {
  78.                         this.mSuggestion = mName + " 女士"  + " 該多一點飯啦~ BMI:" + mBMI;
  79.                 }
  80.                
  81.                 else
  82.                 {
  83.                         this.mSuggestion = "妳確定??" + mBMI;
  84.                 }
  85.                
  86.                 return mSuggestion;
  87.                        
  88.         }

  89. }


  90. public class BMIManager
  91. {
  92.         String mName;
  93.         float mHeight;
  94.         float mWeight;
  95.         boolean mSex;
  96.         float mBMI = 0;
  97.         String mSuggestion;
  98.        
  99.         BMIManager(String n, float h, float w, boolean s)                //建構子
  100.         {
  101.                 this.mName = n;
  102.                 this.mHeight = h;
  103.                 this.mWeight = w;
  104.                 this.mSex = s;
  105.         }
  106.        
  107.         void setName(String n)
  108.         {
  109.                 this.mName = n;
  110.         }
  111.        
  112.         void setHeight(float h)
  113.         {
  114.                 this.mHeight = h;
  115.         }
  116.        
  117.         void setWeight(float w)
  118.         {
  119.                 this.mHeight = w;
  120.         }
  121.        
  122.         void setSex(boolean s)
  123.         {
  124.                 this.mSex = s;
  125.         }
  126.        
  127.         float getHeight()
  128.         {
  129.                 return this.mHeight;
  130.         }
  131.        
  132.         float getWeight()
  133.         {
  134.                 return this.mWeight;
  135.         }
  136.        
  137.         boolean getSex()
  138.         {
  139.                 return this.mSex;
  140.         }
  141.        
  142.         float caculateBMI()
  143.         {
  144.                 float h;
  145.                 h = (this.mHeight/100)*(this.mHeight/100);
  146.                 this.mBMI = this.mWeight/h;
  147.                 return this.mBMI;
  148.         }
  149.        
  150.         String suggestion()
  151.     {
  152.                 if(mBMI==0)
  153.                         caculateBMI();
  154.                 if(mSex)
  155.                 {
  156.                         this.mSuggestion = (new ManSuggestion()).mSuggestion(mBMI, mName);
  157.                 }
  158.                 else
  159.                 {
  160.                         this.mSuggestion = (new WomanSuggestion()).mSuggestion(mBMI, mName);
  161.                 }
  162.                 return this.mSuggestion;
  163.     }
  164. }
複製代碼
  1. import java.util.Scanner;


  2. public class MyBMI
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 Scanner scan = new Scanner(System.in);
  7.                 String name;
  8.                 float h;
  9.                 float w;
  10.                 boolean s;

  11.                 System.out.println("請輸入您的大名 : ");
  12.                 name = scan.next();
  13.                 System.out.println("請輸入您的身高(公分) : ");
  14.                 h = Float.parseFloat(scan.next());
  15.                 System.out.println("請輸入您的體重(公斤) : ");
  16.                 w = Float.parseFloat(scan.next());
  17.                 System.out.println("請輸入您的性別男1/女0 : ");
  18.                 s = Integer.parseInt(scan.next())==1;
  19.                
  20.                 BMIManager myBMI = new BMIManager(name, h, w, s);
  21.                
  22.                 myBMI.caculateBMI();
  23.                 System.out.println(myBMI.suggestion());
  24.         }

  25. }
複製代碼

TOP

返回列表