返回列表 發帖

TQC206 - 四則運算

本帖最後由 tonyh 於 2013-6-15 17:26 編輯
  1. public class tqc206
  2. {
  3.     public static void main(String args[])
  4.     {
  5.       try
  6.       {
  7.           if(args.length==3)
  8.           {
  9.               float a=Float.parseFloat(args[0]);
  10.               float b=Float.parseFloat(args[2]);
  11.               switch(args[1])
  12.               {
  13.                   case "+":
  14.                       System.out.println(a+"+"+b+"="+(a+b));
  15.                       break;
  16.                   case "-":
  17.                       System.out.println(a+"-"+b+"="+(a-b));
  18.                       break;
  19.                   case "*":
  20.                       System.out.println(a+"*"+b+"="+(a*b));
  21.                       break;
  22.                   case "/":
  23.                       if(b==0)
  24.                       {
  25.                           System.out.println("除數不可為0");
  26.                           return;
  27.                       }
  28.                       System.out.println(a+"/"+b+"="+(a/b));
  29.                       break;
  30.                   default:
  31.                       System.out.println("引數格式不對,請使用如下格式\n Calc 1 + 2");
  32.               }
  33.           }else
  34.           {
  35.               System.out.println("引數格式不對,請使用如下格式\n Calc 1 + 2");
  36.           }
  37.       }catch(Exception e)
  38.       {
  39.           System.out.println("引數格式不對,請使用如下格式\n Calc 1 + 2");
  40.       }
  41.     }
  42. }
複製代碼

返回列表