티스토리 뷰

[주제]

- overload 메소드를 활용하여 2개의 정수 or 실수를 계산하는 계산기 구현



[중요]

- 모든 문자는 문자열로 받았기 때문에 메소드에 인자로 보낼 때는 정수형 or 실수형으로 변환 필요

  ※ Integer.parseInt(문자열): 문자열을 정수형으로 변환

  ※ Double.parseDuble(문자열): 문자열을 실수형으로 변환



[소스 코딩]

package calculatorMethod;
 
import java.util.Scanner;
 
class calculatorMethod {
    public static void main(String[] args) {
/*
 * 1. 첫 번째 수 입력
 * 2. 연산자 입력
 * 3. 두 번째 수 입력
 * ※ 모두 문자열로 입력 받음
 *
 * [추가 구현]
 * 정수인지 실수인지 판단
 * ※ charAt 활용
 *
 * [계산 경우의 수]
 * 정수 : 정수 -> 정수형으로 반환
 * 정수 : 실수
 * 실수 : 정수
 * 실수 : 실수
 * ※ Over Load 활용
 */
 
        Scanner scan = new Scanner(System.in);
 
        String num1, num2, oper;
 
        System.out.println("[계산기 프로그램]");
        System.out.print("첫 번째 수를 입력해주세요 = ");
        num1 = scan.next();
 
        boolean num1Judge = numJudge(num1);
 
        System.out.print("연산자(+,-,*,/)를 입력해주세요 = ");
        oper = scan.next();
 
        System.out.print("두 번째 수를 입력해주세요 = ");
        num2 = scan.next();
 
        boolean num2Judge = numJudge(num2);
        
        String result;
        if (num1Judge == true && num2Judge == true) {
            // 정수 : 정수
            result = operator(Integer.parseInt(num1), oper, Integer.parseInt(num2));
        } else if (num1Judge == true && num2Judge == false) {
            // 정수 : 실수
            result = operator(Integer.parseInt(num1), oper, Double.parseDouble(num2));
        } else if (num1Judge == false && num2Judge == true) {
            // 실수 : 정수
            result = operator(Double.parseDouble(num1), oper, Integer.parseInt(num2));
        }
        else {
            // 실수: 실수
            result = operator(Double.parseDouble(num1), oper, Double.parseDouble(num2));
        }
        
        System.out.println(num1 + " " + oper + " " + num2 + " = " + result);
        scan.close();
    }
 
///// 메소드 영역 /////
 
/*
 * 숫자를 문자열로 받았기 때문에 'charAt'을 사용하여 '.'(dot)의
 * 존재여부를 기준으로 '정수'인지 '실수'인지 판단
 */
    public static boolean numJudge(String num) {
        boolean Judge = true;
        for (int i = 0; i < num.length(); i++) {
            if (num.charAt(i) == '.') {
                Judge = false;
                break;
            }
        }
        return Judge;
    }
 
    // '정수 : 정수'를 연산
    public static String operator(int n1, String op, int n2) {
        int res = 0;
        
        switch (op) {
            case "+":
                res = n1 + n2;
                break;
 
            case "-":
                res = n1 - n2;
                break;
 
            case "*":
                res = n1 * n2;
                break;
 
            case "/":
                res = n1 / n2;
                break;
        }
        
        return String.valueOf(res);
    }
 
    // '정수 : 실수'를 연산
    public static String operator(int n1, String op, double n2) {
        double res = 0;
        
        switch (op) {
            case "+":
                res = n1 + n2;
                break;
 
            case "-":
                res = n1 - n2;
                break;
 
            case "*":
                res = n1 * n2;
                break;
 
            case "/":
                res = n1 / n2;
                break;
        }
        
        return String.valueOf(res);
    }
 
    // '실수 : 정수'를 연산
    public static String operator(double n1, String op, int n2) {
        double res = 0;
        
        switch (op) {
            case "+":
                res = n1 + n2;
                break;
 
            case "-":
                res = n1 - n2;
                break;
 
            case "*":
                res = n1 * n2;
                break;
 
            case "/":
                res = n1 / n2;
                break;
        }
        
        return String.valueOf(res);
    }
 
    // '실수 : 실수'를 연산
    public static String operator(double n1, String op, double n2) {
        double res = 0;
        
        switch (op) {
            case "+":
                res = n1 + n2;
                break;
 
            case "-":
                res = n1 - n2;
                break;
 
            case "*":
                res = n1 * n2;
                break;
 
            case "/":
                res = n1 / n2;
                break;
        }
        
        return String.valueOf(res);
    }
}
cs



■ 실행결과



'프로그래밍 언어 > Java(연습)' 카테고리의 다른 글

블랙잭 게임(메소드 연습)  (2) 2016.05.02
야구게임(메소드 연습)  (0) 2016.04.30
wrapper  (0) 2016.04.30
overload  (0) 2016.04.30
가변 인수  (0) 2016.04.29
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함