返回列表 發帖

BMIManager

本帖最後由 ray 於 2012-10-12 20:07 編輯
  1. public class BMIManager
  2. {
  3.         float mHeight;
  4.         float mWeight;
  5.         boolean mSex;
  6.         float mBMI;
  7.         String mSuggestion;
  8.         BMIManager(float h,float w,boolean s)
  9.         {
  10.                 this.mHeight = h;
  11.                 this.mWeight = w;
  12.                 this.mSex = s;
  13.         }
  14.        
  15.         void setHeight(float h)
  16.         {
  17.                 this.mHeight = h;
  18.         }
  19.        
  20.         float getHeight()
  21.         {
  22.                 return this.mHeight;
  23.         }
  24.        
  25.         void setWeight(float w)
  26.         {
  27.                 this.mWeight = w;
  28.         }
  29.        
  30.         float getWeight()
  31.         {
  32.                 return this.mWeight;
  33.         }
  34.        
  35.         void setSex(boolean s)
  36.         {
  37.                 this.mSex = s;
  38.         }
  39.        
  40.         boolean getSex()
  41.         {
  42.                 return this.mSex;
  43.         }
  44.        
  45.         float calculateBMI()
  46.         {
  47.                
  48.                 return mBMI;
  49.         }
  50.        
  51.         String suggestion()
  52.         {
  53.                
  54.                 return mSuggestion;
  55.         }
  56.        
  57. }
複製代碼

  1. Scanner scan = new Scanner(System.in);
  2.                    String yourname;
  3.                    float h;
  4.                    float w;
  5.                    boolean s;
  6.                    float BMI;
  7.                    System.out.println("請輸入您的大名 : ");
  8.                    yourname = scan.next();
  9.                    System.out.println("請輸入您的身高 : ");
  10.                    h = Float.parseFloat(scan.next());
  11.                    h = (h/100);
  12.                    h = (h * h);
  13.                    System.out.println("請輸入您的體重 : ");
  14.                    w = Float.parseFloat(scan.next());
  15.                    System.out.println("請輸入您是男生(1)/女生(0) :");
  16.                    s = Integer.parseInt(scan.next())==1;
  17.                    BMIManager myBMI = new BMIManager(h,w,s);
  18.                   
  19.                    System.out.println("您輸入的大名 :"+yourname);
  20.                    System.out.println("您輸入的身高 :"+myBMI.mHeight);
  21.                    System.out.println("您輸入的體重 :"+myBMI.getWeight());
  22.                    System.out.println("您輸入您是:"+(myBMI.getSex()==true?"男生":"女生"));
  23.                   
複製代碼

TOP

返回列表