냠냠냠

java) 100 이하 소수(Prime Number) 출력하기 본문

언어/JAVA

java) 100 이하 소수(Prime Number) 출력하기

[Nada] 2019. 12. 2. 20:19

문제) 전달된 값을 통해 소수이면 출력하기 (100 이하까지)

 

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
public class MethodJustice {
 
    public static void main(String[] args) {
        boolean tf1;
        int count = 1;
        
        while(count <= 100) {
            
            if(sosu(count))    // 조건이 참이면 실행
                System.out.println(count);   
            
            count++;
        }
            
    }
 
// 소수 판별하는 메소드
    public static boolean sosu(int n1) {
        boolean tf = true;
        
        if(n1 == 1) {
            return !tf; // false 반환
        }
        else if(n1%2 == 1 || n1 == 2) {
            return tf;
        }
        else
            return !tf;
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs
Comments