Prac.java
만약 같은 내용을 5번 찍어내고 싶을 때 어떻게 해야할까?
메소드를 안 썼을 때public class Prac { public static void main(String[] args){ String title = "웹개발의 봄 스프링"; String tutor = "남병관"; int week = 5; float ratings = 5.0f; . System.out.println("제목: "+title); System.out.println("강사: "+tutor); System.out.println("주차: "+week); System.out.println("별점: "+ratings); . System.out.println("제목: "+title); System.out.println("강사: "+tutor); System.out.println("주차: "+week); System.out.println("별점: "+ratings); . System.out.println("제목: "+title); System.out.println("강사: "+tutor); System.out.println("주차: "+week); System.out.println("별점: "+ratings); . System.out.println("제목: "+title); System.out.println("강사: "+tutor); System.out.println("주차: "+week); System.out.println("별점: "+ratings); . System.out.println("제목: "+title); System.out.println("강사: "+tutor); System.out.println("주차: "+week); System.out.println("별점: "+ratings); } }그럼 100번을 찍어줘야 한다면???
차라리 반복 되는 코드를 하나의 메소드를 만들어서 반복하면 어떨까??
메소드 썼을 때public class Prac { . public static void printInfo() { //메소드 /* 1. 중복된 코드들을 한번에 정리해서 반복되는 코드들을 쉽게 사용하기 위해 쓴다 2. 메소드는 높은 재사용의 목적이 있기 때문에 특정성을 부여하면 안된다 3. 메소드 자체는 한번 만들어 놓으면 몇번이고 호출을 할 수 있다 4. 프로그램을 구조화 시키고, 소스코드를 간결하게 만들어준다= 모듈화의 시작.*/ String title = "웹개발의 봄 스프링"; String tutor = "남병관"; int week = 5; float ratings = 5.0f; . System.out.println("제목: "+title); System.out.println("강사: "+tutor); System.out.println("주차: "+week); System.out.println("별점: "+ratings); } . public static void main(String[] args){ for(int i=0; i<100; i++) { printInfo(); } } }
Prac.java
package com.sparta.turtle.prac; . public class Prac { //파라미터 X: (비어있음), 반환값 X: void public static void simplePrint() { //메인문에서 출력하려면 static 필요 System.out.println("파라미터x, 반환값x"); } . // //파라미터 O: (type), 번환값 X: void public static void simpleSum(int a, int b) { System.out.println("num1: " +a+ "num2: " +b); } //파라미터 X: (비어있음), 번환값 O: Int (or String or Double ...) public static int simpleReturn(){ return 3; } . //파라미터 O: (type), 반환값 O: 함수 public static int sum(int num1, int num2) { return num1 + num2; } public static void main(String[] args){ simplePrint(); //바로 실행 simpleSum(1,2); //파라미터 넣어주기 int ret1 = simpleReturn(); //return 결과 받아줄 int 변수 선언 System.out.println(ret1); int ret2 = sum(2,4); //return 값 받아줄 int 변수 선언, 함수에 파라미터 넣어주기 System.out.println(ret2); } }
예제) 계산기 구현하기
- long 타입으로 두개의 수를 받아서 사칙연산 한 값을 반환하는 메소드 만들기
- 나눗셈 연산은 double 타입으로
package com.sparta.turtle.prac; . public class Prac { public static long sum(long a, long b) { return a+b; } . public static long subtract(long a, long b) { return a-b; } . public static long multiply(long a, long b) { return a*b; } . public static double divide(double a, double b) { return a/b; } public static void main(String[] args){ long ret1 = sum(3l,7l); System.out.println(ret1); . long ret2 = subtract(3l,6l); System.out.println(ret2); . long ret3 = multiply(2l,8l); System.out.println(ret3); . double ret4 = divide(8d,3d); System.out.println(ret4); } }결과
10
-3
16
2.6666666666666665
return 값 있다면, 그 값을 받을 변수가 필요하다.