返回列表 發帖

我又有狀況!!~

  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. }
複製代碼
錯誤訊息如下5 r: w  n* v# K/ Q9 w
Exception in thread "main" java.lang.Error: Unresolved compilation problem: ! }+ h1 ]8 N1 _% j0 P2 x2 I
        Syntax error on token "int", invalid ClassType
0 h/ f5 v3 Z. I# `/ [# M
( @! j! w# `/ I$ ~& P8 ~0 x        at TQC207.main(TQC207.java:9)

錯再這一行!?
3 {7 ]- U, \# Kint nums = new int(args.length);

TOP

對啊,兩個錯:1 z3 }2 l: p& O2 Q7 o" y+ N
1.陣列是用中括號[]不是()2 f( o( P- a+ Y# p. [' ^
2.既然是陣列,前面的宣告也要是陣列:int[]
! S  I, a$ [2 o$ S; w6 ^這一行正確的寫法如下:/ R0 l; ?3 @  j2 x! w5 [* S- P  E
int[] nums = new int[args.length];

TOP

oh~ i see
! `/ \/ }# f/ S7 F* @' dthanks!!

TOP

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

TOP

加入偶數判斷式~~; p9 G# y6 s3 ~% c4 f
public class TQC207 {
0 H" S% ]9 ]7 _. a$ S% W6 @) d" d% x- Z, {
        public static void main(String[] args) {
# d5 u1 |. ^0 d& q, d                int sum = 0;+ V6 q* J3 C+ C5 t
                int odd = 0;" a# ]7 L$ \. J5 e  y
                int max = 0;: v/ Q; p+ |# @! b2 s
                int tmp = 0;: E- ]. G+ K! d
                int even = 0;
4 N0 [# Q9 L. J* g1 H+ k% s9 s( w                int[] nums = new int[args.length];& C& V# _' ^! A) P% C- T( W
                System.out.println("請輸入需要判斷的數: (數字間須留空白!!)");
" y; E' u  i4 r1 U9 _- R                try {8 c6 K1 K8 l  r: `3 c6 b
                        for (int i = 0; i < args.length; i++) {6 K# D* a0 p4 ?$ {
                                tmp = Integer.parseInt(args[i]);
7 M. `) D% e6 n  s! y                                nums[i] = tmp;
. w% _6 |" P/ x0 M7 z                                sum += tmp;
. }" V0 T' `  ~                                if (tmp % 2 == 0) {
% c4 G! {" ^6 `. q6 Y                                        even++;# T5 h# K0 H& i& `
                                }
5 J6 y" N0 R# F                                if (tmp % 2 != 0) {
8 O# P! q6 U4 m# ~                                        odd++;
) @% z5 W% s2 h                                }
& {1 d0 ~& m  Z* p                                max = Math.max(tmp, max);. _2 P, R& c- j. t  Q! y
                        }
7 ]: E& c: H! y" v! r                        System.out.println("最大值" + max);; b5 W; F* S% @& W# s- Q4 @  e" ]) n9 v
                        System.out.println("奇數" + odd);5 \0 s* x7 r$ o
                        System.out.println("偶數" + even);
8 `3 u* Y* ]5 J$ O8 [                        System.out.println("總和" + sum);
# c# o& T" ?# K9 V' ^                } catch (Exception e) {
% ?$ O* v& w3 R  v$ e/ N                        System.out.println("輸入錯誤");2 g9 R3 i7 c/ ~  j" I
                }3 J8 w3 @8 V, `: L3 L+ N( |  o* d
) P0 _9 _- B/ T1 F7 v" ^
        }* H3 K4 ]" v' b6 B# L0 h4 q

0 V! Q! N7 A" }}

TOP

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

TOP

返回列表