返回列表 發帖
  1. import java.util.Scanner;

  2. class ManSuggestion
  3. {
  4.          String suggestion(float mBMI)
  5.      {         
  6.                 String mSuggestion = "";
  7.                 if(mBMI >= 35)
  8.                 {
  9.                         mSuggestion = "重度肥胖,你是神豬嗎 ?";
  10.                 }
  11.                 else if(mBMI >= 30 && mBMI <35)
  12.                 {
  13.                         mSuggestion = "中度肥胖,恭喜你可以請人家幫忙綁鞋帶!";
  14.                 }
  15.                 else if(mBMI >= 27 && mBMI <30)
  16.                 {
  17.                         mSuggestion = "過重,低頭快看不到你的腳趾頭了!";
  18.                 }
  19.                 else if(mBMI >= 24 && mBMI <27)
  20.                 {
  21.                         mSuggestion = "稍重,恭喜你還可以蹲下去綁鞋帶!";
  22.                 }
  23.                 else if(mBMI >= 18.5 && mBMI <24)
  24.                 {
  25.                         mSuggestion = "標準,帥哥一枚保持下去!";
  26.                 }
  27.                 else if(mBMI < 18.5)
  28.                 {
  29.                         mSuggestion = "過輕!!,紙片人!";
  30.                 }
  31.                 return mSuggestion;
  32.      }

  33. }

  34. class WomanSuggestion
  35. {
  36.          String suggestion(float mBMI)
  37.      {         
  38.                 String mSuggestion = "";
  39.                 if(mBMI >= 35)
  40.                 {
  41.                         mSuggestion = "不要每年都咬橘子壓!";
  42.                 }
  43.                 else if(mBMI >= 30 && mBMI <35)
  44.                 {
  45.                         mSuggestion = "太豐腴了 , 擋住我的電視了!";
  46.                 }
  47.                 else if(mBMI >= 27 && mBMI <30)
  48.                 {
  49.                         mSuggestion = "有點豐腴喔 , 注意一下飲食喔!";
  50.                 }
  51.                 else if(mBMI >= 24 && mBMI <27)
  52.                 {
  53.                         mSuggestion = "體重有點重,少吃一點會更好喔!";
  54.                 }
  55.                 else if(mBMI >= 18.5 && mBMI <24)
  56.                 {
  57.                         mSuggestion = "標準喔,正妹一個喔!";
  58.                 }
  59.                 else if(mBMI < 18.5)
  60.                 {
  61.                         mSuggestion = "過輕,再多吃一點會更好喔!";
  62.                 }
  63.                 return mSuggestion;
  64.      }

  65. }

  66. public class BMIManager {
  67.     private float mHeight;   
  68.     private float mWeight;
  69.     private String mSex;
  70.     private float mBMI=0;
  71.     String msuggestion;
  72.    
  73.     void setHeight(float h)
  74.     {
  75.             this.mHeight = h;
  76.     }
  77.    
  78.     float getHeight()
  79.     {
  80.             return this.mHeight;
  81.     }
  82.    
  83.     void setWeight(float w)
  84.     {
  85.             this.mWeight = w;
  86.     }
  87.    
  88.     float getWeight()
  89.     {
  90.             return this.mWeight;
  91.     }

  92.     void setSex(String s)
  93.     {
  94.             this.mSex=s;                                      
  95.     }
  96.    
  97.     String getSex()
  98.     {
  99.            
  100.             if (mSex.equals("M"))
  101.                     mSex="男士";                   
  102.             else if(mSex.equals("F"))
  103.                 mSex="女士";
  104.             else
  105.                     mSex="無法判別";
  106.            
  107.             return this.mSex;           
  108.     }
  109.    
  110.   
  111.        
  112.         BMIManager(float h , float w , String s)
  113.         {
  114.                 this.mHeight=h;
  115.                 this.mWeight=w;
  116.                 this.mSex=s;               
  117.         }
  118.    
  119.         float calculateBMI()
  120.         {
  121.                 if (mBMI==0)
  122.                 {
  123.                   mBMI = (mWeight/((mHeight/100)*(mHeight/100)));
  124.                 }
  125.                 return mBMI;
  126.         }
  127.        
  128.         String suggestion()
  129.         {
  130.                   if(mBMI == 0)
  131.               calculateBMI();
  132.                                             
  133.                   if (mSex.equals("男士"))                  
  134.                           return (new ManSuggestion()).suggestion(mBMI);  
  135.               else
  136.                       return (new WomanSuggestion()).suggestion(mBMI);                     
  137.         }
  138.        
  139.        
  140.        
  141. }

  142. import java.util.Scanner;


  143. public class BMIManagerTest {

  144.        
  145.         public static void main(String[] args) {
  146.                
  147.                
  148.                       String name,sex;
  149.                       float hight;
  150.                       float weight;
  151.                      
  152.                       Scanner scan = new Scanner(System.in);

  153.                       System.out.print("請輸入您的姓名=");
  154.                       name=scan.next();

  155.                       System.out.print("請輸入你的身高(cm)=");
  156.                       hight=Float.parseFloat(scan.next());

  157.                       System.out.print("請輸入你的體重(kg)=");
  158.                       weight=Float.parseFloat(scan.next());
  159.                      
  160.                       System.out.print("請輸入你的性別 M or F =");
  161.                       sex=scan.next();

  162.                       BMIManager myBMI = new BMIManager (hight , weight , sex);                     
  163.                                                           
  164.               System.out.println("親愛的 "+name+" "+myBMI.getSex()+" ,您的BMI="+myBMI.calculateBMI());
  165.               System.out.println(myBMI.suggestion());   
  166.               
  167.               
  168.         }

  169. }

  170. 請輸入您的姓名=kkman
  171. 請輸入你的身高(cm)=170
  172. 請輸入你的體重(kg)=99
  173. 請輸入你的性別 M or F =M
  174. 親愛的 kkman 男士 ,您的BMI=34.256054
  175. 中度肥胖,恭喜你可以請人家幫忙綁鞋帶!
複製代碼

TOP

返回列表