返回列表 發帖

為什麼錯,請老師解答!

本帖最後由 lon 於 2012-8-18 09:46 編輯 ! w# x9 f( @' g6 ^
  1. public class jva206 {
  2.         public static void main(String[] args) {
  3.                 try{
  4.                 if(args.length==3){
  5.                         float a=Float.parseFloat(args[0]);
  6.                         float b=Float.parseFloat(args[2]);
  7.                         switch(args[1]){
  8.                         case"+":
  9.                                 System.out.println(a+"+"+b+"="+(a+b));
  10.                                 break;
  11.                         case"-":
  12.                                 System.out.println(a+"-"+b+"="+(a-b));
  13.                                 break;
  14.                         case"x":
  15.                                 System.out.println(a+"*"+b+"="+(a*b));
  16.                                 break;
  17.                         case"/":
  18.                                 if(b==0){
  19.                                         System.out.println("除數不可為0");
  20.                                         System.exit(0);
  21.                                 }
  22.                                 System.out.println(a+"/"+b+"="+(a/b));
  23.                                 break;
  24.                         default:
  25.                                 System.out.println("引數格式不對,請使用如下格式\n Calc 1 + 2");
  26.                                 break;
  27.                         }
  28.                 }else{
  29.                         System.out.println("引數格式不對,請使用如下格式\n Calc 1 + 2");
  30.                         System.exit(0);
  31.                 }
  32.                 }catch(Exception e){
  33.                         System.out.println("引數格式不對,請使用如下格式\n Calc 1 + 2");
  34.                         System.exit(0);
  35.                 }
  36.         }
  37. }
複製代碼
陳彥綸

switch裡面要用字串的話,JDK必須要使用1.7以上,還有case後面不該緊接著判斷字串,應該要加一個空白,例如:case  "+":  A7 x8 i8 z8 `4 ^
因為你或許不能掌握考試時JDK的版本,switch-case那邊我建議你用if-else-if取代,程式範例如下:
0 U( m2 U, c: x7 y
! u6 \) Q, R" g  [# w5 ypublic class TQC206
# ?+ `  U' V" A+ Q& [{
4 I' x7 f, H1 t/ f( r7 \   public static void main(String args[])/ g' A1 V3 O4 l, ]
    {
3 H- q8 t% y! w; |" j) ?3 s- H      float a,b;9 f0 o) |" k3 N  H/ y% E+ I  o( w% Z  H5 Z
      String calc;
6 ^( R6 k4 k6 J; j* C; }9 N0 D      try{
3 D# S$ w- R$ f8 L6 l% F: S9 B- l         if(args.length==3){
& ]/ h( x8 o% C7 ]+ h            a = Float.parseFloat(args[0]);
. B* P, w3 ^0 o" p            b = Float.parseFloat(args[2]);' Y" @+ ^1 b. K; ]
            calc = args[1];0 h' G1 h) a+ m( O
            if(calc.equals("+")){3 M; `& O  u1 M0 D) f" N
               System.out.println(args[0]+args[1]+args[2]+"="+(a+b));
5 |+ }; a) E( B4 Z% p            }else if(calc.equals("-")){2 `5 _% {! V4 I3 {# @
               System.out.println(args[0]+args[1]+args[2]+"="+(a-b));" _" Q0 H1 C0 F+ @
            }else if(calc.equals("x")){
4 }( L1 e# @+ v$ O               System.out.println(args[0]+args[1]+args[2]+"="+(a*b));
; ~7 T4 H/ I5 c) e2 C            }else if(calc.equals("/"))
, G: D( a( n" s, h8 B; q3 \            {
/ U0 ]2 @+ K9 m. Q; |2 }0 D" k               if(b==0)
; A4 s; }! e2 J5 ~- f               {1 u1 v7 X* a2 }5 H3 D  w8 b
                 System.out.println("除數不可為 0");
% Y* A( G- ^0 W# m( O               }else3 I2 m- L  k1 ?6 O4 h% |1 v
               {9 F3 I0 n3 x5 S0 Q9 m% U1 Y
                  System.out.println(args[0]+args[1]+args[2]+"="+(a/b));0 q  d/ S/ s. N* K( c; }
               }
; l+ \' f: U2 _; G, s8 A            }
) P0 z; \, Z8 d5 h: R            else+ P! W5 G7 f$ K$ U+ _0 i
            {
: T' |& L; G. ~+ Z/ i               System.out.println("第二個引數請使用 + - x / 的其中一種運算符號");
7 j# `$ F* U' Z               System.exit(0);
% z% x) j' A# L) H            }
! _: X, B/ i5 F) `& x         }else{; \) b& \; r1 C; g: E8 j
            System.out.println("請使用三個引數,如下格式\n 1 + 2");
& ]9 L. J+ V) b9 H  M: n         }
5 e* u3 ]' c8 V) k" g      }3 K0 M% a0 D# T- \' t
      catch(ArrayIndexOutOfBoundsException e)
$ M6 |1 Q2 s- q( R, m      {
9 y2 v# c" [. K: s               System.out.println("使用引數不對,請使用如下格式\n 1 + 2\n"+e.getMessage());
6 s6 W5 F* X/ `3 `      }
) _. r) z7 \5 C  l, A% s      catch(NumberFormatException e)4 @. r& [) U8 R) q  ?" I
      {
5 m& s* C$ d# c+ m0 q% _, X3 D7 V         System.out.println("使用引數不對,請使用如下格式\n 1 + 2\n"+e.getMessage());
2 t6 V: o2 B% H2 M& Z. R      }
5 @. s+ h- V' q      2 }+ V  ^6 j( F
    }: L, K/ v  o0 h- L: n$ _
}

TOP

返回列表