-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProb1.java
More file actions
56 lines (39 loc) · 909 Bytes
/
Prob1.java
File metadata and controls
56 lines (39 loc) · 909 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package chap06;
public class Prob1 {
public static void main(String[] args) {
int[] ia = new int[] { 3, 7, 1, 8, 10, 2, 15, 2, 10 };
int[] ib = new int[] { 1, 2, 3, 4, 5 };
Prob1 obj = new Prob1();
obj.calcArr(ia);
obj.calcArr(ib);
int[] arr = obj.oddArr();
for (int i : arr) {
System.out.print(i);
}
}
public void calcArr(int[] arr) {
// 배열의 합을 구해서 출력
int oddHap = 0;
int evenHap = 0;
for (int i = 0 ; i < arr.length; i++) {
if (arr[i] % 2 == 0 ) {
evenHap = evenHap + arr[i];
} else {
oddHap = oddHap + arr[i];
}
}
System.out.println("홀수의합:"+oddHap);
System.out.println("짝수의합:"+evenHap);
}
public int[] oddArr() {
int[] arr = new int[10];
int num = 1 ;
for (int i = 0; i <arr.length; i++) {
if ( num % 2 != 0 ) {
arr[i/2] = num;
}
num++;
}
return arr;
}
}