返回列表 發帖

我又有狀況!!~

  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. }
複製代碼
錯誤訊息如下8 ?. s3 ]3 Q' ^! {7 |0 J# M
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
6 Q. ~. f/ C' r7 U: ?# `! C        Syntax error on token "int", invalid ClassType
  c4 n# E# G: a; R& P2 C
$ Z1 s# X$ n8 v+ d8 d' j1 a        at TQC207.main(TQC207.java:9)

錯再這一行!?
3 H+ z- i  \( F& ]: N" M/ Wint nums = new int(args.length);

TOP

對啊,兩個錯:
" e3 d& N) C. G' c1.陣列是用中括號[]不是()3 ~7 ]% G! g& B5 ]- q) ^
2.既然是陣列,前面的宣告也要是陣列:int[]" I0 y3 o! K0 f& Y$ h0 j1 a9 m, P
這一行正確的寫法如下:
( S7 k# q  Y" S: A9 Kint[] nums = new int[args.length];

TOP

oh~ i see
2 i9 x+ ~' y5 F4 D  k, ethanks!!

TOP

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

TOP

加入偶數判斷式~~
  K+ V3 |+ X6 bpublic class TQC207 {3 H( ?$ x4 \9 }" L

) F3 G, z" o. Z$ d" `. O        public static void main(String[] args) {
. x- W5 y" ^2 F" ~/ z- o3 f                int sum = 0;
  f% ?7 {1 G9 I4 o% a- N# c8 I                int odd = 0;
5 F/ C/ v* x2 l7 \                int max = 0;; E0 V" ?- ]0 s, D" j/ [
                int tmp = 0;
) M7 R' {, ?. ~' q% }" U                int even = 0;
( _' W+ L) P, L! t4 s6 l5 u8 ]                int[] nums = new int[args.length];8 d1 Y( F. t8 k- O; |, J* E
                System.out.println("請輸入需要判斷的數: (數字間須留空白!!)");
& i" P" o4 C* s8 r7 J                try {
& P$ l3 O; a  [  @" V/ k5 H                        for (int i = 0; i < args.length; i++) {
; x* j8 V9 Q- q6 H                                tmp = Integer.parseInt(args[i]);
: {8 S, u- }4 T9 `0 r% [( [                                nums[i] = tmp;
+ t3 X1 V4 W1 u' f                                sum += tmp;6 e3 I' X6 o% L, x  V* n
                                if (tmp % 2 == 0) {/ M6 q# Z1 r; H8 {2 t6 U% c
                                        even++;/ i8 H- _. `2 U% t# a) V% T
                                }1 t8 z7 D- j2 m
                                if (tmp % 2 != 0) {0 Y4 t" E3 a; B& R! h/ R
                                        odd++;% l6 V, l; _5 n9 G! z- w1 L" L
                                }. t4 i2 W+ A1 _; D% ~
                                max = Math.max(tmp, max);1 I0 H) O1 ]) K. w; [
                        }9 l8 h! R' k& o
                        System.out.println("最大值" + max);
% T: H: |! H5 V/ i                        System.out.println("奇數" + odd);
, _! Q( Q8 v6 z                        System.out.println("偶數" + even);( V, r! @+ C8 U: e
                        System.out.println("總和" + sum);
$ @! [4 b+ Z3 r% P& T/ F; A# D                } catch (Exception e) {
% d) c, m2 P4 [                        System.out.println("輸入錯誤");
3 L* m) D, B" E" c                }
8 k- b8 \7 [7 Q; u4 x6 u* L8 d) I3 t/ i8 \
        }# W* v6 S! {* U

: A: ~: h5 g/ |6 Q}

TOP

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

TOP

返回列表