標題:
BMIManager
[打印本頁]
作者:
ray
時間:
2012-10-12 19:59
標題:
BMIManager
本帖最後由 ray 於 2012-10-12 20:07 編輯
public class BMIManager
{
float mHeight;
float mWeight;
boolean mSex;
float mBMI;
String mSuggestion;
BMIManager(float h,float w,boolean s)
{
this.mHeight = h;
this.mWeight = w;
this.mSex = s;
}
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()
{
return mBMI;
}
String suggestion()
{
return mSuggestion;
}
}
複製代碼
作者:
ray
時間:
2012-10-12 21:06
Scanner scan = new Scanner(System.in);
String yourname;
float h;
float w;
boolean s;
float BMI;
System.out.println("請輸入您的大名 : ");
yourname = scan.next();
System.out.println("請輸入您的身高 : ");
h = Float.parseFloat(scan.next());
h = (h/100);
h = (h * h);
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.println("您輸入的大名 :"+yourname);
System.out.println("您輸入的身高 :"+myBMI.mHeight);
System.out.println("您輸入的體重 :"+myBMI.getWeight());
System.out.println("您輸入您是:"+(myBMI.getSex()==true?"男生":"女生"));
複製代碼
作者:
吳尚哲
時間:
2012-10-14 21:01
本帖最後由 吳尚哲 於 2012-10-14 21:12 編輯
public class BMIManager
{
String mName;
float mHeight;
float mWeight;
boolean mSex;
float mBMI;
String mSuggestion;
BMIManager(String n, float h, float w, boolean s) //建構子
{
this.mName = n;
this.mHeight = h;
this.mWeight = w;
this.mSex = s;
}
void setName(String n)
{
this.mName = n;
}
void setHeight(float h)
{
this.mHeight = h;
}
void setWeight(float w)
{
this.mHeight = w;
}
void setSex(boolean s)
{
this.mSex = s;
}
float getHeight()
{
return this.mHeight;
}
float getWeight()
{
return this.mWeight;
}
boolean getSex()
{
return this.mSex;
}
float caculateBMI()
{
this.mBMI = this.mWeight/((this.mHeight/100)*(this.mHeight/100));
return this.mBMI;
}
String suggestion()
{
if(this.mBMI >= 35)
{
this.mSuggestion = this.mName + "重度肥胖";
}
else if(this.mBMI >= 30 && this.mBMI < 35)
{
this.mSuggestion = this.mName + (mSex?"先生":"女士") +"中度肥胖 BMI:" + this.mBMI;
}
else if(this.mBMI >= 27 && this.mBMI < 30)
{
this.mSuggestion = this.mName + (mSex?"先生":"女士") + "輕度肥胖 BMI:" + this.mBMI;
}
else if(this.mBMI >= 24 && this.mBMI < 27)
{
this.mSuggestion = this.mName + (mSex?"先生":"女士") + "過重 BMI:" + this.mBMI;
}
else if(this.mBMI >= 18.5 && this.mBMI < 24)
{
this.mSuggestion = this.mName + (mSex?"先生":"女士") + "標準 BMI:" + this.mBMI;
}
else if(this.mBMI < 18.5)
{
this.mSuggestion = this.mName + (mSex?"先生":"女士") + "過輕 BMI:" + this.mBMI;
}
else
{
this.mSuggestion = "你確定??" + this.mBMI;
}
return this.mSuggestion;
}
}
複製代碼
import java.util.Scanner;
public class MyBMI {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String name;
float h;
float w;
boolean s;
System.out.println("請輸入您的大名 : ");
name = 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(name, h, w, s);
myBMI.caculateBMI();
System.out.println(myBMI.suggestion());
}
}
複製代碼
請輸入您的大名 :
阿哲
請輸入您的身高(公分) :
170
請輸入您的體重(公斤) :
78
請輸入您的性別男1/女0 :
1
阿哲先生過重 BMI:26.989618
複製代碼
老師我想請教一下
建構子的功能是在物件實體化的過程中將內容初始化而已嗎?
除此之外還有其他用意嗎?
和用set方法給予初始值有什麼不一樣?
作者:
晏有聰
時間:
2012-10-15 03:15
public class BMIManager {
float mHeight;
float mWeight;
String mSex;
float mBMI;
String msuggestion;
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(String s)
{
this.mSex=s;
}
String getSex()
{
if (mSex.equals("M"))
mSex="男士";
else if(mSex.equals("F"))
mSex="女士";
else
mSex="無法判別";
return this.mSex;
}
BMIManager(float h , float w , String s)
{
this.mHeight=h;
this.mWeight=w;
this.mSex=s;
}
float calculateBMI(float h , float w)
{
mBMI = (w/((h/100)*(h/100)));
return mBMI;
}
String suggestion()
{
if (mBMI<18.5)
{
msuggestion="體重過輕喔!!";
}
else if(mBMI>=18.5 && mBMI<24)
{
msuggestion="恭喜,正常範圍喔!!";
}
else if(mBMI>=24 && mBMI<27)
{
msuggestion="過重!!";
}
else if(mBMI>=27 && mBMI<30)
{
msuggestion="輕度肥胖!!";
}
else if(mBMI>=30 && mBMI<35)
{
msuggestion="中度肥胖!!";
}
else if(mBMI>=35)
{
msuggestion="重度肥胖!!";
}
else
{
msuggestion="輸入錯誤喔!!";
}
return msuggestion;
}
public static void main(String[] args) {
}
}
import java.util.Scanner;
public class BMIManagerTest {
public static void main(String[] args) {
String name,sex;
float hight;
float weight;
Scanner scan = new Scanner(System.in);
System.out.print("請輸入您的姓名=");
name=scan.next();
System.out.print("請輸入你的身高(cm)=");
hight=Float.parseFloat(scan.next());
System.out.print("請輸入你的體重(kg)=");
weight=Float.parseFloat(scan.next());
System.out.print("請輸入你的性別 M or F =");
sex=scan.next();
BMIManager myBMI = new BMIManager (hight , weight , sex);
System.out.println("親愛的 "+name+" "+myBMI.getSex()+" ,您的BMI="+myBMI.calculateBMI(hight, weight));
System.out.println(myBMI.suggestion());
}
}
請輸入您的姓名=knight
請輸入你的身高(cm)=170
請輸入你的體重(kg)=66
請輸入你的性別 M or F =M
親愛的 knight 男士 ,您的BMI=22.83737
恭喜,正常範圍喔!!
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2