返回列表 發帖
  1. import java.util.*;
  2. public class ch54
  3. {
  4.     public static void main(String args[])throws BodyException
  5.     {
  6.         for(;;)
  7.         {
  8.             try
  9.             {
  10.                 Scanner s=new Scanner(System.in);
  11.                 float h,w;
  12.                 System.out.print("請輸入你的身高(cm): ");
  13.                 h=(s.nextFloat())/100;
  14.                 if(h*100<50 || h*100>220)
  15.                     throw new BodyException();
  16.                 System.out.print("請輸入你的體重(kg): ");
  17.                 w=s.nextFloat();
  18.                 if(w<20 || w>200)
  19.                     throw new BodyException();
  20.                 System.out.println("你的BMI值為: "+w/(h*h));
  21.                 return;
  22.             }
  23.             catch(InputMismatchException e)
  24.             {
  25.                 System.out.println("請輸入數字!");
  26.             }
  27.             catch(BodyException e)
  28.             {
  29.                 System.out.println("請輸入合理範圍內的身高或體重!");
  30.             }
  31.         }
  32.     }
  33. }
  34. class BodyException extends Exception
  35. {
  36.     public BodyException()
  37.     {
  38.         super();
  39.     }
  40. }
複製代碼

TOP

返回列表