返回列表 發帖

我又有狀況!!~

  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. }
複製代碼
錯誤訊息如下
7 P5 U- o$ Z6 y6 CException in thread "main" java.lang.Error: Unresolved compilation problem:
) {7 s4 {/ P; s        Syntax error on token "int", invalid ClassType$ ?8 n! f  L  P- E0 R
6 v  u) Q# k$ \, N( @
        at TQC207.main(TQC207.java:9)

錯再這一行!?) \# K" Z7 y0 |2 |% K5 S5 A' x; l
int nums = new int(args.length);

TOP

對啊,兩個錯:
5 R# w. u( q5 m, W3 J! _1 O1.陣列是用中括號[]不是()/ q9 T! o5 Y4 @& z
2.既然是陣列,前面的宣告也要是陣列:int[]$ k8 X& e5 D4 s% I3 D2 F
這一行正確的寫法如下:0 s* f) x1 }1 }( h+ h0 U6 n
int[] nums = new int[args.length];

TOP

oh~ i see5 `0 Z9 {% W$ Q; s& C* z. r! ~
thanks!!

TOP

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

TOP

加入偶數判斷式~~
7 ]$ f8 Y$ x( C4 Y$ ypublic class TQC207 {
9 Y4 m5 E6 J9 p% t0 F* B; r: m! ~, }5 I
        public static void main(String[] args) {$ z& {& w3 M8 p+ x9 G7 u6 g+ i. f
                int sum = 0;  s, z9 h4 \8 A( G7 |7 y  h
                int odd = 0;: ]; O' Q& Q8 t
                int max = 0;7 Q6 a8 B/ s- P5 U* f1 M. a
                int tmp = 0;
. W+ A2 A% J' v2 o                int even = 0;
7 E9 o0 Z- x; ?* k                int[] nums = new int[args.length];
( ]1 m1 _1 V$ {' L+ c1 i: x2 S# I# b                System.out.println("請輸入需要判斷的數: (數字間須留空白!!)");, z& \3 ?: W( b
                try {
4 Y1 u" D. P7 l7 \2 G, |                        for (int i = 0; i < args.length; i++) {
' O- ~( w5 z, \7 v9 k                                tmp = Integer.parseInt(args[i]);/ h/ ~1 a& O# V! n
                                nums[i] = tmp;& ?! w! H) a* b5 S) A0 l9 J: Z! W$ u
                                sum += tmp;' E. S1 ~) ]. b
                                if (tmp % 2 == 0) {
  U2 f% q+ H8 r$ N' Z4 r5 i                                        even++;
* `+ `- e1 H+ y4 G                                }$ ^0 @) p+ B, M/ O1 `& _3 S
                                if (tmp % 2 != 0) {
  J3 w2 j! w6 Z9 M2 N. S- j                                        odd++;8 h1 |7 M5 [$ f/ e
                                }
+ f1 h/ M" L9 m                                max = Math.max(tmp, max);  v. E; J  A* ]" T, @3 M
                        }
* i, \# v$ |  B6 y5 N2 A4 z$ ~/ J                        System.out.println("最大值" + max);0 x- J9 T/ O' D; Y- Q1 L
                        System.out.println("奇數" + odd);
) w* f3 t: n: p0 v  P3 U7 N$ }                        System.out.println("偶數" + even);
, ]1 y" H. p2 a  f5 @! s/ u                        System.out.println("總和" + sum);
9 ]% t) d, d: ^. @                } catch (Exception e) {: t' _9 N& V3 w+ u% B( R
                        System.out.println("輸入錯誤");
4 z% o7 ~# g4 i  B4 H                }
& ^9 O3 ~& Z& T* m9 p' h: P( H: @) B3 K) K- q( k! `
        }
/ ]: i) [6 \; K! ?0 F6 W: F4 g2 @6 U5 S8 R/ L9 l
}

TOP

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

TOP

返回列表