-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlusMinus.java
More file actions
44 lines (35 loc) · 1.17 KB
/
Copy pathPlusMinus.java
File metadata and controls
44 lines (35 loc) · 1.17 KB
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
package org.example.quiz;
/**
* @Author : yion
* @Date : 2017. 6. 16.
* @Description : 주어진 배열의 음수, 양수, 0의 갯수를 파악하여 비율로 표시하라. 단, 소수점 6자리까지 표현한다.
*/
public class PlusMinus {
public static void main(String[] args) {
int[] numbers = {-4, 3, -9, 0, 4, 1};
float[] items = itemCount(numbers, numbers.length);
System.out.printf("%.6f\n", items[0]);
System.out.printf("%.6f\n", items[1]);
System.out.printf("%.6f\n", items[2]);
}
private static float[] itemCount(int[] numbers, int length) {
int positives = 0;
int negatives = 0;
int zeros = 0;
for (int i = 0; i < length; i++) {
if (numbers[i] > 0) {
positives++;
} else if (numbers[i] < 0) {
negatives++;
} else {
zeros++;
}
}
float point = (float) length;
float positive = positives / point;
float negative = negatives / point;
float zero = zeros / point;
float[] results = {positive, negative, zero};
return results;
}
}