返回列表 發帖
  1. public class BMIManager {
  2.     float mHeight;   
  3.     float mWeight;
  4.     String mSex;
  5.     float mBMI;
  6.     String msuggestion;
  7.    
  8.     void setHeight(float h)
  9.     {
  10.             this.mHeight = h;
  11.     }
  12.    
  13.     float getHeight()
  14.     {
  15.             return this.mHeight;
  16.     }
  17.    
  18.     void setWeight(float w)
  19.     {
  20.             this.mWeight = w;
  21.     }
  22.    
  23.     float getWeight()
  24.     {
  25.             return this.mWeight;
  26.     }

  27.     void setSex(String s)
  28.     {
  29.             this.mSex=s;                                      
  30.     }
  31.    
  32.     String getSex()
  33.     {
  34.            
  35.             if (mSex.equals("M"))
  36.                     mSex="男士";                   
  37.             else if(mSex.equals("F"))
  38.                 mSex="女士";
  39.             else
  40.                     mSex="無法判別";
  41.            
  42.             return this.mSex;           
  43.     }
  44.    
  45.   
  46.        
  47.         BMIManager(float h , float w , String s)
  48.         {
  49.                 this.mHeight=h;
  50.                 this.mWeight=w;
  51.                 this.mSex=s;               
  52.         }
  53.    
  54.         float calculateBMI(float h , float w)
  55.         {
  56.                 mBMI = (w/((h/100)*(h/100)));
  57.                 return mBMI;
  58.         }
  59.        
  60.         String suggestion()
  61.         {
  62.                 if (mBMI<18.5)
  63.                   {
  64.                         msuggestion="體重過輕喔!!";
  65.                   }
  66.               else if(mBMI>=18.5 && mBMI<24)                      
  67.               {
  68.                       msuggestion="恭喜,正常範圍喔!!";
  69.               }
  70.               else if(mBMI>=24 && mBMI<27)
  71.               {
  72.                       msuggestion="過重!!";
  73.               }
  74.               else if(mBMI>=27 && mBMI<30)
  75.               {
  76.                       msuggestion="輕度肥胖!!";
  77.               }
  78.               else if(mBMI>=30 && mBMI<35)
  79.               {
  80.                       msuggestion="中度肥胖!!";
  81.               }
  82.               else if(mBMI>=35)
  83.               {
  84.                       msuggestion="重度肥胖!!";
  85.               }
  86.               else
  87.               {
  88.                       msuggestion="輸入錯誤喔!!";
  89.               }               
  90.                 return msuggestion;
  91.         }
  92.        
  93.         public static void main(String[] args) {
  94.                
  95.        
  96.                
  97.                
  98.                

  99.         }

  100. }

  101. import java.util.Scanner;


  102. public class BMIManagerTest {

  103.        
  104.         public static void main(String[] args) {
  105.                
  106.                
  107.                       String name,sex;
  108.                       float hight;
  109.                       float weight;
  110.                      
  111.                       Scanner scan = new Scanner(System.in);

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

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

  116.                       System.out.print("請輸入你的體重(kg)=");
  117.                       weight=Float.parseFloat(scan.next());
  118.                      
  119.                       System.out.print("請輸入你的性別 M or F =");
  120.                       sex=scan.next();

  121.                       BMIManager myBMI = new BMIManager (hight , weight , sex);
  122.                                                           
  123.               System.out.println("親愛的 "+name+" "+myBMI.getSex()+" ,您的BMI="+myBMI.calculateBMI(hight, weight));
  124.               System.out.println(myBMI.suggestion());
  125.               
  126.                      
  127.                      
  128.         }

  129. }

  130. 請輸入您的姓名=knight
  131. 請輸入你的身高(cm)=170
  132. 請輸入你的體重(kg)=66
  133. 請輸入你的性別 M or F =M
  134. 親愛的 knight 男士 ,您的BMI=22.83737
  135. 恭喜,正常範圍喔!!
複製代碼

TOP

返回列表