標題:
BMManager.java
[打印本頁]
作者:
ray
時間:
2012-10-26 19:06
標題:
BMManager.java
package ray.work.bmicalc;
import java.util.Scanner;
class Suggestion
{
String suggestion(float mBMI)
{
if(mBMI == 0)
return "input error";
else
return "your BMI is" + mBMI;
}
}
class ManSuggestion extends Suggestion
{
String suggestion(float mBMI)
{
String mSuggestion = "";
if(mBMI >= 35)
{
mSuggestion = "這麼肥!會不會太扯了你~";
}
else if(mBMI >= 30 && mBMI <35)
{
mSuggestion = "再不減肥我怕你坐飛機要買2張票了";
}
else if(mBMI >= 27 && mBMI <30)
{
mSuggestion = "少吃點吧!胖子!";
}
else if(mBMI >= 24 && mBMI <27)
{
mSuggestion = "過重";
}
else if(mBMI >= 18.5 && mBMI <24)
{
mSuggestion = "標準";
}
else if(mBMI < 18.5)
{
mSuggestion = "過輕";
}
return mSuggestion;
}
}
class WomanSuggestion extends Suggestion
{
String suggestion(float mBMI)
{
String mSuggestion = "";
if(mBMI >= 35)
{
mSuggestion = "....一切盡在不言中";
}
else if(mBMI >= 30 && mBMI <35)
{
mSuggestion = "聽說XX牌減肥食品不錯!";
}
else if(mBMI >= 27 && mBMI <30)
{
mSuggestion = "加油!再瘦一點你就是大美女了!";
}
else if(mBMI >= 24 && mBMI <27)
{
mSuggestion = "過重";
}
else if(mBMI >= 18.5 && mBMI <24)
{
mSuggestion = "標準";
}
else if(mBMI < 18.5)
{
mSuggestion = "過輕";
}
return mSuggestion;
}
}
class BMIManager
{
private float mHeight;
private float mWeight;
private boolean mSex;
private float mBMI;
//private String mSuggestion;
private Suggestion mSuggestion;
BMIManager(float h,float w,boolean s)
{
this.mHeight = h;
this.mWeight = w;
this.mSex = s;
this.mBMI = 0;
if(this.mSex)
this.mSuggestion = new ManSuggestion();
else
this.mSuggestion = new WomanSuggestion();
}
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();
return this.mSuggestion.suggestion(mBMI);
}
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?"男生":"女生"));
*/
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2