返回列表 發帖

我又有狀況!!~

  1. public class TQC207 {

  2.         public static void main(String[] args) {
  3.        int sum = 0;
  4.        int odd = 0;
  5.        int max = 0;
  6.        int tmp = 0;
  7.        int nums = new int(args.length);
  8.        try
  9.        {
  10.                for(int i = 0; i < args.length; i++)
  11.                {
  12.                       tmp = Integer.parseInt(args[i]);
  13.                       nums [i] = tmp;
  14.                       sum += tmp;
  15.                       if(tmp % 2 != 0)
  16.                       {
  17.                             odd++;
  18.                       }
  19.                       max = Math.max(tmp,max);
  20.                }
  21.                System.out.println("最大值" + max );
  22.                System.out.println("奇數" + odd );
  23.                System.out.println("總和" + sum );
  24.        }
  25.        catch(Exception e)
  26.        {
  27.             System.out.println("輸入錯誤");   
  28.        }
  29.       
  30.         }

  31. }
複製代碼
錯誤訊息如下
3 ~. f; ^* v' V: o% xException in thread "main" java.lang.Error: Unresolved compilation problem: + ?0 x- n% R* g5 B0 _4 M  E5 |
        Syntax error on token "int", invalid ClassType+ b* `# T- p; z: E
( @8 A8 W% A8 }; ^
        at TQC207.main(TQC207.java:9)

錯再這一行!?
: V* h$ ?3 q( h. V# Mint nums = new int(args.length);

TOP

對啊,兩個錯:
/ J2 q& f! Q; k' o+ i! A3 B1.陣列是用中括號[]不是()
+ ]4 A2 ^- z6 @" K+ J) n2.既然是陣列,前面的宣告也要是陣列:int[]* B/ M9 R$ Z2 R- B
這一行正確的寫法如下:
! b8 o, b' K) Bint[] nums = new int[args.length];

TOP

oh~ i see9 ]* T; f9 U3 J* m5 _
thanks!!

TOP

那如果在nums後宣告也可以摟!!

TOP

加入偶數判斷式~~: ]/ Z+ z- M5 p% v4 _6 v
public class TQC207 {" t; r) p* x& m, F0 X- Q' j) K

& e& _3 @( [6 C7 k2 f6 M        public static void main(String[] args) {. `5 y/ o5 ^( {
                int sum = 0;
2 c  p7 [( j7 T                int odd = 0;; R: Q/ z8 z4 a* k1 O  j0 ^
                int max = 0;! d$ t) F. s9 F6 {& J
                int tmp = 0;! Z4 s6 L/ X7 c0 U. ~
                int even = 0;+ z. l" i- `- B# e0 B5 V% g
                int[] nums = new int[args.length];1 R; f: d! A- ^) }( B7 y# o
                System.out.println("請輸入需要判斷的數: (數字間須留空白!!)");
3 Q0 ]1 N. E' a7 }; [8 U                try {7 f- S; l3 W( {  d
                        for (int i = 0; i < args.length; i++) {
3 a! T" p  P" D8 ^0 a" n% p3 @  ~                                tmp = Integer.parseInt(args[i]);; P0 A0 x1 @5 h$ X
                                nums[i] = tmp;! o3 H, K6 X, K- n- `
                                sum += tmp;
+ G9 I. A! J6 q9 k                                if (tmp % 2 == 0) {! ]' R. ~, }3 Q( ]+ c9 {% x# J7 _
                                        even++;/ C# r4 }9 P7 }  q' d" K3 @
                                }
, D# V! |: `8 r. W" i  \                                if (tmp % 2 != 0) {/ F5 {. I% a' Y5 x1 B, V2 w
                                        odd++;5 e. m: o( f: Z0 J3 ^
                                }
% q0 ?# c4 C+ ^- k( t0 D7 t                                max = Math.max(tmp, max);
: x5 ^: R2 j* K                        }
& h  f% o3 _" E, @  C                        System.out.println("最大值" + max);4 j: }7 [0 n9 B6 _" \0 Y$ |/ s
                        System.out.println("奇數" + odd);6 {' e' m5 \  d9 V
                        System.out.println("偶數" + even);
1 w+ \7 T  E+ B, w( I$ t                        System.out.println("總和" + sum);/ u% [- P, m) _
                } catch (Exception e) {, \+ d( U( n9 c8 h( t. o
                        System.out.println("輸入錯誤");, W# o% b/ Q8 H! J$ E* H4 r0 `  R
                }
$ ?" r7 V  x3 J4 \& @) _6 D8 o4 d, i
  R3 f9 A* G, `        }
8 q, b' t. ~% L* W7 J) T8 s
' P6 ^% A& Y/ M}

TOP

加入偶數判斷式~~有錯誤嗎?我看起來沒問題啊!!

TOP

返回列表