返回列表 發帖
  1. import java.util.*;
  2. public class B {

  3.         public static void main(String[] args) {
  4.         for(;;)
  5.         {
  6.             try
  7.             {
  8.                 double h,w,bmi;
  9.                 Scanner s=new Scanner(System.in);
  10.                 System.out.print("請輸入你的身高(公分): ");
  11.                 h=s.nextDouble();
  12.                 if(h<50 || h>250)
  13.                     throw new MyException("請輸入合理的身高!");
  14.                 h/=100;  
  15.                 System.out.print("請輸入你的體重(公斤): ");
  16.                 w=s.nextDouble();
  17.                 if(w<5 || w>250)
  18.                     throw new MyException("請輸入合理的體重!");
  19.                 bmi=w/(h*h);
  20.                 System.out.printf("你的BMI值為: %.2f",bmi);
  21.             }catch(InputMismatchException e)
  22.             {
  23.                 System.out.println("請輸入數字!");
  24.             }catch(MyException e)
  25.             {
  26.                 System.out.println(e.getMessage());
  27.             }
  28.             System.out.println();
  29.         }
  30.     }
  31. }
  32. class MyException extends Exception
  33. {
  34.     MyException(String str)
  35.     {
  36.         super(str);
  37.     }
  38. }
複製代碼

TOP

返回列表