參考解答:- double weight=0 , height = 0 , BMI = 0; //用來存放體重,身高與BMI值
- char sex = 'F'; //用來存放性別
-
- System.out.println("請輸入您的體重:");
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String bufstr = br.readLine();
- weight = Double.parseDouble(bufstr);
-
- System.out.println("請輸入您的身高(以公尺為單位):");
- bufstr = br.readLine();
- height = Double.parseDouble(bufstr);
-
-
- System.out.println("請輸入您的性別(M/F):");
- bufstr = br.readLine();
- sex = bufstr.charAt(0);
-
- //依據性別設定最大與最小BMI值
- int BMI_MIN=18,BMI_MAX=22;
- if (sex == 'M')
- {
- BMI_MIN=20;
- BMI_MAX=25;
- }
- else
- {
- //Do nothing
- }
-
- //計算BMI值
- BMI = weight / (height*height);
- System.out.println("您的BMI值為: " + BMI);
-
- //判斷BMI值是否正常
- if( BMI > BMI_MAX)
- {
- System.out.println("您的BMI值為超過標準,請多運動");
- }
- else if( BMI > BMI_MIN )
- {
- System.out.println("您的BMI值為標準範圍,請保持");
- }
- else
- {
- System.out.println("您的BMI值為低於標準,請多進食");
- }
複製代碼 |