返回列表 發帖

我又有狀況!!~

  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. }
複製代碼
錯誤訊息如下6 L; R2 w' n% S- p2 s3 d
Exception in thread "main" java.lang.Error: Unresolved compilation problem: ; _7 y; T3 U3 f, ~
        Syntax error on token "int", invalid ClassType; s8 L5 I4 a9 L5 t% L( A

/ X2 t2 \2 a9 _: o: u3 ^1 `, X        at TQC207.main(TQC207.java:9)

錯再這一行!?
$ R* h3 G% y, G* Mint nums = new int(args.length);

TOP

對啊,兩個錯:5 E$ B, o$ ?5 N+ T  L/ r( x
1.陣列是用中括號[]不是()
& @% m8 i9 r$ r8 b" E5 L* F& m2.既然是陣列,前面的宣告也要是陣列:int[]. S  d. B. |  s$ `% ?$ ^! D
這一行正確的寫法如下:( L: Q) ]$ `' {* u3 }. `* I# _8 o0 v
int[] nums = new int[args.length];

TOP

oh~ i see( S! m( L4 S9 X# M/ e
thanks!!

TOP

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

TOP

加入偶數判斷式~~9 a. G9 C( U1 p* g
public class TQC207 {, c3 l$ F# F6 T0 p
. x& c  U9 _, B
        public static void main(String[] args) {5 z+ S* W* R+ D. \
                int sum = 0;& D+ _# y. [- d0 `
                int odd = 0;  p, w; ?: Y* l. Z; j2 {
                int max = 0;, N" T( k2 Z* c. j2 T5 t
                int tmp = 0;
9 H6 e* A0 r* y6 B7 e! n/ w                int even = 0;3 w3 O1 `% X5 g
                int[] nums = new int[args.length];9 t7 e% i3 @  L! e0 [& j
                System.out.println("請輸入需要判斷的數: (數字間須留空白!!)");
, ~6 l" ?6 g( ^/ o) X                try {
/ p3 Y/ O; u2 J& t8 N+ D" R& ~                        for (int i = 0; i < args.length; i++) {# T2 T# x) R! T# L
                                tmp = Integer.parseInt(args[i]);6 o* w8 }) K. o3 t* U$ J) V: K
                                nums[i] = tmp;% _. y8 B- s( ]8 `$ E1 \
                                sum += tmp;) K; r# P; f, C9 L
                                if (tmp % 2 == 0) {1 r/ G( W# W" m$ L6 t* g2 Y
                                        even++;
. s- l2 \+ [/ d6 l$ `6 W                                }
$ e3 _) H. [( w/ n7 p( T2 m9 M) g                                if (tmp % 2 != 0) {) x# L" m) h+ t- Y; C
                                        odd++;; |& I! g% z$ @/ r' A
                                }* E% S/ D- m. c0 C1 E1 I3 x
                                max = Math.max(tmp, max);
% F! Q& A5 k0 m) p7 `; H0 M( ^                        }2 O- e! U; I; H6 Z
                        System.out.println("最大值" + max);  a% \  n  a/ n5 b9 L
                        System.out.println("奇數" + odd);
/ ?4 e+ U7 N- O" n/ }0 i$ {                        System.out.println("偶數" + even);1 V. ]- d1 |- T  K& l# k7 h
                        System.out.println("總和" + sum);
1 ~+ o9 ^7 U/ `( w! ]2 h                } catch (Exception e) {/ t9 ~) a1 w! e; `1 c; \' B
                        System.out.println("輸入錯誤");: h8 D7 w  a- G4 q
                }
/ J' _/ T. ^8 _
6 l; m% @7 \: g        }* ~  K0 h3 I; O' B

- e9 B% |- d7 f& @6 h: j}

TOP

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

TOP

返回列表