返回列表 發帖

我又有狀況!!~

  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. }
複製代碼
錯誤訊息如下
, T" V. G0 W6 L0 ~- H# KException in thread "main" java.lang.Error: Unresolved compilation problem: $ d8 \: k8 Q: \
        Syntax error on token "int", invalid ClassType( k: j0 V( q. n" n% C& t4 R
* k! h' m: F+ v7 P4 o
        at TQC207.main(TQC207.java:9)

錯再這一行!?7 y  c. O& j$ W! s% i, |
int nums = new int(args.length);

TOP

oh~ i see- O1 i& J: ?% Q% R: ]0 D- E4 R9 B
thanks!!

TOP

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

TOP

加入偶數判斷式~~; H  F* o! T/ k- Z' y
public class TQC207 {
8 r3 {% w9 P' M5 f) M; }) n' y; {5 ?; f
        public static void main(String[] args) {
! x" X" J( b& N1 t2 m8 l, s                int sum = 0;$ a2 Q1 n' S- e4 a+ p2 b
                int odd = 0;) ]. R9 G9 T1 |& E- d( b0 L
                int max = 0;
' E& g1 {; F  f( _$ _: z6 H                int tmp = 0;+ L4 O2 Y6 j6 d6 i0 G) x
                int even = 0;" v! u5 @+ K. ^+ f
                int[] nums = new int[args.length];$ |% V+ E% u% l
                System.out.println("請輸入需要判斷的數: (數字間須留空白!!)");# B: H4 }/ ^. }' }% G+ c5 k
                try {# B" y" E. a7 H- p( @
                        for (int i = 0; i < args.length; i++) {
3 Q' d0 t% z* k4 H$ }' }                                tmp = Integer.parseInt(args[i]);
6 Y2 q" z8 G4 q1 C4 `3 R, z3 W                                nums[i] = tmp;9 z. a4 E7 b' ~+ n0 E
                                sum += tmp;2 d1 u6 v6 l% ~8 q
                                if (tmp % 2 == 0) {4 U) {) K5 ~- ~7 ]
                                        even++;
/ f; A4 B5 H* E# b. P- n' h                                }
( p& R$ v8 ?) P  o                                if (tmp % 2 != 0) {
9 s, ^: B2 g0 x3 }* R$ O                                        odd++;
) V$ i5 ~& G6 B                                }7 X5 R$ T- _4 O+ `5 r- I
                                max = Math.max(tmp, max);
0 C3 R4 _* k3 S* M. H, Z, U6 B6 Z                        }
5 W0 `' G- ?. q& s- P& ]9 R$ M# [                        System.out.println("最大值" + max);
, z# ?' j- e( f1 A                        System.out.println("奇數" + odd);
4 x; T3 W( S  S% O  @                        System.out.println("偶數" + even);
$ T# h5 D8 a4 ^. ^6 z                        System.out.println("總和" + sum);5 t' J; U2 l+ r- G8 q9 U% n" u6 l
                } catch (Exception e) {2 Z3 g4 U7 D# w6 P& u% j
                        System.out.println("輸入錯誤");
6 H/ P) W# ]6 c7 }+ b% h3 t                }
+ I; j5 l, ]8 p3 _/ K5 U0 p; E- p' K' T2 E
        }6 u2 R) W) V/ E5 ~4 q: j+ U
* j, B' c2 D! a6 e
}

TOP

返回列表