-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProb003_random.java
More file actions
56 lines (46 loc) · 1.37 KB
/
Copy pathProb003_random.java
File metadata and controls
56 lines (46 loc) · 1.37 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
45
46
47
48
49
50
51
52
53
54
55
56
package java0905_api.prob;
/*[문제]
* 2~20 사이의 난수 10개를 발생시켜 배열에 저장한 후에 리턴하는
* makeArray() 메서드와 배열에서 소수를 구하여 출력하는 primeNumber() 메서드를 각각 구현하시오.
* [출력결과]
<< 발생된 난수 >>
* 15 7 4 4 8 7 2 11 17 5
<< 소수 숫자 >>
* 7 7 2 11 17 5
* */
import java.util.Random;
public class Prob003_random {
public static void main(String[] args) {
int[] array = makeArray();
System.out.println("<< 발생된 난수 >>");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println("\n<< 소수 숫자 >>");
primeNumber(array);
}// end main( )
private static int[] makeArray() {
// 난수값를 발생시킨후 반환하는 프로그램을 구현하시오.
Random rn = new Random();
int[] num = new int[10];
for (int i = 0; i < num.length; i++) {
num[i] = rn.nextInt(20) + 1;
}
return num;
}// end makeArray()
public static void primeNumber(int[] array) {
// array배열에서 소수만 출력하는 프로그램을 구현하시오.
int cnt = 0;
for (int i = 0; i < array.length; i++) {
for (int j = 2; j < array[i]; j++) {
if (array[i] % j == 0) {
cnt++;
}
}
if (cnt == 0) {
System.out.print(" " + array[i]);
}
cnt = 0;
}
}// end primeNumber()
}// end class