[자바 메서드] 28. 리턴함수를 이용한 계산기 프로그램

김건우's avatar
Feb 05, 2025
[자바 메서드] 28. 리턴함수를 이용한 계산기 프로그램
package ex04; public class Cal01 { static int add(int a, int b) { return a + b; } static int sub(int a, int b) { return a - b; } static int mul(int a, int b) { return a * b; } static int div(int a, int b) { return a / b; }

위의 코드를 이용해 문제 풀이

public static void main(String[] args) { // 문제 : 5+4, 결과*2, 결과/3, 결과-5 (최종 결과값을 출력) // 5+4 int a = add(5, 4); System.out.println("a의 결과값 : " + a); // 결과 * 2 int b = mul(a, 2); System.out.println("b의 결과값: " + b); // 결과/3 int c = div(b, 3); System.out.println("c의 결과값: " + c); // 결과-5 int d = sub(c, 5); System.out.println("최종 결과값: " + d); } }
Share article

gunwoo