返回列表 發帖

20121015

import java.util.Scanner;

class BMIManager
{
        private float mHeight;
        private float mWeight;
        private boolean mSex;
        private float mBMI;
        private String mSuggestion;
        BMIManager(float h,float w,boolean s)
        {
                this.mHeight = h;
                this.mWeight = w;
                this.mSex = s;
                this.mBMI = 0;
        }
        void setHeight(float h)
        {
                this.mHeight = h;
        }
       
        float getHeight()
        {
                return this.mHeight;
        }
       
        void setWeight(float w)
        {
                this.mWeight = w;
        }
       
        float getWeight()
        {
                return this.mWeight;
        }
       
        void setSex(boolean s)
        {
                this.mSex = s;
        }
       
        boolean getSex()
        {
                return this.mSex;
        }
       
        float calculateBMI()
        {
                if(this.mBMI == 0)
                {
                        float h = this.mHeight/100;
                        h = h * h;
                        this.mBMI = this.mWeight/h;
                }
                return this.mBMI;
        }
       
        String suggestion()
        {
           if(mBMI == 0)
                   calculateBMI();
          
           if(this.mBMI >= 35)
           {
                   mSuggestion = "重度肥胖";
           }
           else if(this.mBMI >= 30 && this.mBMI <35)
           {
                   mSuggestion = "中度肥胖";
           }
           else if(this.mBMI >= 27 && this.mBMI <30)
           {
                   mSuggestion = "輕度肥胖";
           }
           else if(this.mBMI >= 24 && this.mBMI <27)
           {
                   mSuggestion = "過重";
           }
           else if(this.mBMI >= 18.5 && this.mBMI <24)
           {
                   mSuggestion = "標準";
           }
           else if(this.mBMI < 18.5)
           {
                   mSuggestion = "過輕";
           }
           return mSuggestion;
        }
       
        public static void main(String[] args)
        {
                   Scanner scan = new Scanner(System.in);
                   String yourname;
                   float h;
                   float w;
                   boolean s;
                   System.out.println("請輸入您的大名 : ");
                   yourname = scan.next();
                   System.out.println("請輸入您的身高 : ");
                   h = Float.parseFloat(scan.next());
                   System.out.println("請輸入您的體重 : ");
                   w = Float.parseFloat(scan.next());
                   System.out.println("請輸入您是男生(1)/女生(0) :");
                   s = Integer.parseInt(scan.next())==1;
                   BMIManager myBMI = new BMIManager(h,w,s);
                   System.out.print(myBMI.calculateBMI()+"\n");
                   System.out.printf("%s",myBMI.suggestion());
                  
                   /*
                   System.out.println("您輸入的大名 :"+yourname);
                   System.out.println("您輸入的身高 :"+myBMI.mHeight);
                   System.out.println("您輸入的體重 :"+myBMI.getWeight());
                   System.out.println("您輸入您是:"+(myBMI.getSex()==true?"男生":"女生"));
                   */
                  
        }
       
}

  1. import java.util.Scanner;

  2. class Suggestion
  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 BMIManager
  35. {
  36.         private float mHeight;
  37.         private float mWeight;
  38.         private boolean mSex;
  39.         private float mBMI;
  40.         private String mSuggestion;
  41.        
  42.         BMIManager(float h,float w,boolean s)
  43.         {
  44.                 this.mHeight = h;
  45.                 this.mWeight = w;
  46.                 this.mSex = s;
  47.                 this.mBMI = 0;
  48.         }
  49.        
  50.         void setHeight(float h)
  51.         {
  52.                 this.mHeight = h;
  53.         }
  54.        
  55.         float getHeight()
  56.         {
  57.                 return this.mHeight;
  58.         }
  59.        
  60.         void setWeight(float w)
  61.         {
  62.                 this.mWeight = w;
  63.         }
  64.        
  65.         float getWeight()
  66.         {
  67.                 return this.mWeight;
  68.         }
  69.        
  70.         void setSex(boolean s)
  71.         {
  72.                 this.mSex = s;
  73.         }
  74.        
  75.         boolean getSex()
  76.         {
  77.                 return this.mSex;
  78.         }
  79.        
  80.         float calculateBMI()
  81.         {
  82.                 if(this.mBMI == 0)
  83.                 {
  84.                         float h = this.mHeight/100;
  85.                         h = h * h;
  86.                         this.mBMI = this.mWeight/h;
  87.                 }
  88.                 return this.mBMI;
  89.         }
  90.        
  91.         String suggestion()
  92.         {
  93.                 if(mBMI == 0)
  94.                            calculateBMI();
  95.                 return (new Suggestion()).suggestion(mBMI);
  96.         }
  97.        
  98.         public static void main(String[] args)
  99.         {
  100.                    Scanner scan = new Scanner(System.in);
  101.                    String yourname;
  102.                    float h;
  103.                    float w;
  104.                    boolean s;
  105.                    System.out.println("請輸入您的大名 : ");
  106.                    yourname = scan.next();
  107.                    System.out.println("請輸入您的身高 : ");
  108.                    h = Float.parseFloat(scan.next());
  109.                    System.out.println("請輸入您的體重 : ");
  110.                    w = Float.parseFloat(scan.next());
  111.                    System.out.println("請輸入您是男生(1)/女生(0) :");
  112.                    s = Integer.parseInt(scan.next())==1;
  113.                    BMIManager myBMI = new BMIManager(h,w,s);
  114.                    System.out.print(myBMI.calculateBMI()+"\n");
  115.                    System.out.printf("%s",myBMI.suggestion());
  116.                   
  117.                    /*
  118.                    System.out.println("您輸入的大名 :"+yourname);
  119.                    System.out.println("您輸入的身高 :"+myBMI.mHeight);
  120.                    System.out.println("您輸入的體重 :"+myBMI.getWeight());
  121.                    System.out.println("您輸入您是:"+(myBMI.getSex()==true?"男生":"女生"));
  122.                    */
  123.                   
  124.         }
  125.        
  126. }
複製代碼

TOP

返回列表