加入偶數判斷式~~9 t- g) Z$ ?5 J6 j& i" p: u
public class TQC207 {
' A) B% X, Q; \3 t3 p
+ U, Y4 e; ^7 n, w2 O; q public static void main(String[] args) {
0 m4 X* ?" l( @4 t8 H% g int sum = 0;
4 }: r7 A! x5 ?$ h int odd = 0;9 }) B4 j+ J, c" S* N; T. X& P
int max = 0;
! ]: h3 v$ X6 j$ w6 i5 h3 I, h int tmp = 0;0 `$ A: w! j, Q+ j8 e$ t
int even = 0;7 Y0 q( ?& @( T. p% \
int[] nums = new int[args.length];
! f& Y: m- }/ O System.out.println("請輸入需要判斷的數: (數字間須留空白!!)");
8 _& @" P. N4 _1 |5 c try {. ` b! [. c: X3 r4 @, M0 y
for (int i = 0; i < args.length; i++) {0 b* v) p% n' O O; M; S3 i
tmp = Integer.parseInt(args[i]);: o! \: P( D7 G
nums[i] = tmp;0 f7 N9 o* P5 e7 D$ |
sum += tmp;
( c% k9 _3 T! b- X/ t7 Q if (tmp % 2 == 0) {; l" q" [+ h/ \2 P
even++;
7 E# y. G8 z8 j5 b0 t: n }+ |$ q% ~) N! S# T! t+ m$ z# f
if (tmp % 2 != 0) {
. w" c& C" B8 S9 X/ `5 F H odd++;
1 f& t/ H7 C x3 ]0 ? }1 d8 O! d7 t8 W" m" }
max = Math.max(tmp, max);3 {- b9 @# M1 }9 O
}
: ^. K {9 E7 ^/ P. e1 n' E% `1 H System.out.println("最大值" + max);
" N5 Y' M/ w$ [3 h0 v System.out.println("奇數" + odd);
# O4 V+ d9 P5 R5 w: s System.out.println("偶數" + even);
0 |2 @4 [) m1 d8 Y, e System.out.println("總和" + sum);
2 n; S0 [5 k) c } catch (Exception e) {
+ K- }' m! A; ~2 g System.out.println("輸入錯誤");
( H7 J! I8 }# S0 y }! M/ t M4 c. D6 e' P
. U" R) O, L) n4 a: [ }7 p+ X; x$ K d- y! e X% w
8 X% q! X1 _7 F6 }3 o0 {} |