- import java.util.*;
- public class B {
- public static void main(String[] args) {
- for(;;)
- {
- try
- {
- double h,w,bmi;
- Scanner s=new Scanner(System.in);
- System.out.print("請輸入你的身高(公分): ");
- h=s.nextDouble();
- if(h<50 || h>250)
- throw new MyException("請輸入合理的身高!");
- h/=100;
- System.out.print("請輸入你的體重(公斤): ");
- w=s.nextDouble();
- if(w<5 || w>250)
- throw new MyException("請輸入合理的體重!");
- bmi=w/(h*h);
- System.out.printf("你的BMI值為: %.2f",bmi);
- }catch(InputMismatchException e)
- {
- System.out.println("請輸入數字!");
- }catch(MyException e)
- {
- System.out.println(e.getMessage());
- }
- System.out.println();
- }
- }
- }
- class MyException extends Exception
- {
- MyException(String str)
- {
- super(str);
- }
- }
複製代碼 |