본문 바로가기

Java

[자바 알고리즘] 팩토리얼 Factorial.java

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
57
58
59
60
61
62
63
64
65
66
package part2algo;
 
public class Test {
      /**
       * 팩토리얼 + 스태틱 예제
       * [결과]
       * 첫번째 연산결과:12.0
       * 두번째 연산결과28.0
       * 세번째 연산결과 누적값:55
       * 100단이상은 지원하지 않습니다다
       * 80단 출력
       */
      public static void main(String[] args) {
            Systemout.println"첫번째 연산결과:" + Engine.operator(5.07.0'+'));
            Systemout.println"두번째 연산결과" + Engine.operator(700.048.0'%'));
            Systemout.println"세번째 연산결과 누적값:" + Engine.factorial(10));
            Engine. printGugudan(101);
            Engine. printGugudan(80);
      }
}
class Engine{
      static void printGugudan( int dan) { // 매개변수에 전달하는 값 = 전달인자
             if ( dan > 100) {
                  Systemout.println"100단이상은 지원하지 않습니다다" );
                   return;
                   // 만약 100이 넘으면그냥 끝내고 다음 메서드로 이동시키겠다
            }
             for ( int i = 2; i < 10; i++) {
                  Systemout.println( dan + "*" + i + "=" + ( dan * i));
            }
            Systemout.println();
      }
      // 팩토리얼 값을 반환하는 사용자정의 메서드
      static int factorial( int value) {
             int result = 0;
             for ( int i = 0; i <= value; i++) {
                   result += i;
            }
             return result;
      }
      static double operator( double x, double y, char op) {
             double result = 0;
             switch ( op) {
             case '+':
                   result = x + y;
                   break;
             case '-':
                   result = x - y;
                   break;
             case '*':
                   result = x * y;
                   break;
             case '/':
                   result = x / y;
                   break;
             case '%':
                   result = x % y;
                   break;
             default:
                   result = 0.0;
            }
             return result;
      }
}
 
 
cs