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

  2. class Suggestion
  3. {
  4.         String suggestion_man(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.         String suggestion_woman(float mBMI)
  35.         {         
  36.                    String mSuggestion = "";
  37.                    if(mBMI >= 35)
  38.                    {
  39.                            mSuggestion = " 小姐重度肥胖";
  40.                    }
  41.                    else if(mBMI >= 30 && mBMI <35)
  42.                    {
  43.                            mSuggestion = " 小姐中度肥胖";
  44.                    }
  45.                    else if(mBMI >= 27 && mBMI <30)
  46.                    {
  47.                            mSuggestion = " 小姐輕度肥胖";
  48.                    }
  49.                    else if(mBMI >= 24 && mBMI <27)
  50.                    {
  51.                            mSuggestion = " 小姐過重";
  52.                    }
  53.                    else if(mBMI >= 18.5 && mBMI <24)
  54.                    {
  55.                            mSuggestion = " 小姐標準";
  56.                    }
  57.                    else if(mBMI < 18.5)
  58.                    {
  59.                            mSuggestion = " 小姐過輕";
  60.                    }
  61.                    return mSuggestion;
  62.         }
  63. }

  64. class BMIManager
  65. {
  66.         private float mHeight;
  67.         private float mWeight;
  68.         private boolean mSex;
  69.         private float mBMI;
  70.         private String mSuggestion;
  71.         
  72.         BMIManager(float h,float w,boolean s)
  73.         {
  74.                 this.mHeight = h;
  75.                 this.mWeight = w;
  76.                 this.mSex = s;
  77.                 this.mBMI = 0;
  78.         }
  79.         
  80.         void setHeight(float h)
  81.         {
  82.                 this.mHeight = h;
  83.         }
  84.         
  85.         float getHeight()
  86.         {
  87.                 return this.mHeight;
  88.         }
  89.         
  90.         void setWeight(float w)
  91.         {
  92.                 this.mWeight = w;
  93.         }
  94.         
  95.         float getWeight()
  96.         {
  97.                 return this.mWeight;
  98.         }
  99.         
  100.         void setSex(boolean s)
  101.         {
  102.                 this.mSex = s;
  103.         }
  104.         
  105.         boolean getSex()
  106.         {
  107.                 return this.mSex;
  108.         }
  109.         
  110.         float calculateBMI()
  111.         {
  112.                 if(this.mBMI == 0)
  113.                 {
  114.                         float h = this.mHeight/100;
  115.                         h = h * h;
  116.                         this.mBMI = this.mWeight/h;
  117.                 }
  118.                 return this.mBMI;
  119.         }
  120.         
  121.         String suggestion()
  122.         {
  123.                 if(mBMI == 0)
  124.                            calculateBMI();
  125.                
  126.                 //性別判斷布林比較               
  127.                 if (mSex == true)
  128.                 {
  129.                
  130.                     return (new Suggestion()).suggestion_man(mBMI);
  131.                 }
  132.                 else
  133.                 {
  134.                         return (new Suggestion()).suggestion_woman(mBMI);
  135.                 }
  136.         }

  137.         /*
  138.         public static void main(String[] args)
  139.         {
  140.                    Scanner scan = new Scanner(System.in);
  141.                    String yourname;
  142.                    float h;
  143.                    float w;
  144.                    boolean s;
  145.                    System.out.println("請輸入您的大名 : ");
  146.                    yourname = scan.next();
  147.                    System.out.println("請輸入您的身高 : ");
  148.                    h = Float.parseFloat(scan.next());
  149.                    System.out.println("請輸入您的體重 : ");
  150.                    w = Float.parseFloat(scan.next());
  151.                    System.out.println("請輸入您是男生(1)/女生(0) :");
  152.                    s = Integer.parseInt(scan.next())==1;
  153.                    BMIManager myBMI = new BMIManager(h,w,s);
  154.                    System.out.print(myBMI.calculateBMI()+"\n");
  155.                    System.out.printf("%s",myBMI.suggestion());
  156.                   

  157.                    System.out.println("您輸入的大名 :"+yourname);
  158.                    System.out.println("您輸入的身高 :"+myBMI.mHeight);
  159.                    System.out.println("您輸入的體重 :"+myBMI.getWeight());
  160.                    System.out.println("您輸入您是:"+(myBMI.getSex()==true?"男生":"女生"));

  161.                   
  162.         }
  163.         */
  164.         
  165. }
複製代碼

TOP

  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 yourname;
  8.             float h;
  9.             float w;
  10.             boolean s;
  11.             System.out.println("請輸入您的大名 : ");
  12.             yourname = 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.             BMIManager myBMI = new BMIManager(h,w,s);
  20.             System.out.print(myBMI.calculateBMI()+"\n");
  21.             //System.out.printf("%s",myBMI.suggestion());
  22.             
  23.             System.out.print(yourname + myBMI.suggestion());
  24.             
  25.         }

  26. }
複製代碼
回復 4# 陳建志

TOP

返回列表