리턴값이 없는 메소드: void
package oop0314;
public class Test06_method {
//메소드 작성 영역
//void 리턴값이 없다
public static void test1() {
System.out.println("JAVA");
}//test1() end
public static void test2() {
System.out.println("PYTHON");
return; //함수를 호출한 시점으로 되돌아 간다
//마지막 return명령어는 생략 가능하다
}//test2() end
public static void test3(int a) {//매개변수(parameter)의 자료형은 생략할 수 없다
System.out.println(a);
return;
}//test3() end
public static void test4(int a, int b, int c) {//매개변수는 개별적으로 선언한다
System.out.println(a+b+c);
return;
}//test4() end
public static void test5(double a, double b) {
System.out.println(a+b);
return;
}//test5() end
public static void test6(char c, byte n) {
for(int a=1; a<=n; a++) {
System.out.print(c);
}//for end
}//test6() end
public static void main(String[] args) {
//Method 메소드
//함수, function, 프로시저
//1. 리턴값이 없는 함수
//1) 전달값(argument value)이 없는 경우
test1(); //함수호출
test2();
test1();
//2) 전달값(argument value)이 있는 경우
test3(10);
test4(20, 30, 40);
test5(1.2, 3.4);
//문제) #기호를 100번 출력하시오
byte num=100;
char ch='#';
test6(ch, num);
}//main() end
}//class end
리턴값이 있는 메소드, 재귀적 함수 호출
package oop0315;
public class Test01_method {
public static int test1(int a,int b) {
int c=a+b;
//return; 리턴값이 없다(void)
return c; //리턴값은 1개만 리턴할 수 있다
//리턴값이 있는 경우 리턴값의 자료형을 void자리에 쓴다
}//test1() end
public static String test2(int a) {
if(a%2==0) {
return "짝수";
}else {
return "홀수";
}//if end
}//test2() end
public static boolean test3(int y) {
if(y%4==0 && y%100!=0 || y%400==0) {
return true;
}else {
return false;
}//if end
}//test3() end
public static long test4(int n) {
long gop=1;
for(int a=n; a>=1; a--) {
gop=gop*a;
}//for end
return gop;
}//test4() end
public static long fact(int n) {
if(n==0) {
return 1;
}else {
return n*fact(n-1); //재귀적 함수 호출
}//if end
}//fact() end
public static void main(String[] args) {
//2. 리턴값이 있는 경우
System.out.println(Math.abs(-3));
System.out.println(Math.max(5, 7));
int result=test1(2, 4);
System.out.println(result);
//값 : 상수값, 변수값, 리턴값
System.out.println(test1(5, 6));
//짝수, 홀수 출력하기
String str=test2(7);
System.out.println(str);
//윤년확인하기
if(test3(2022)) {
System.out.println("윤년");
}else {
System.out.println("평년");
}//if end
//팩토리얼 구하기
long f=test4(5);
System.out.println(f);
///////////////////////////////
//3. 재귀적 함수 호출
//팩토리얼값 구하기
System.out.println(fact(5));
}//main() end
}//class end
메소드 호출 방식
- Call by value 값에 의한 호출방식
- Call by reference 참조(주소)에 의한 호출방식
package oop0315;
import java.util.Arrays;
public class Test03_method {
public static void test1(int a, int b) {
System.out.println(a);
System.out.println(b);
}//test1() end
public static void test2(int[] a) {
for(int i=0; i<a.length; i++) {
System.out.println(a[i]);
}//for end
}//test2() end
public static void test3(String a, String b) {
System.out.println(a);
System.out.println(b);
}//test3() end
public static void test4(String[] a) {
for(int i=0; i<a.length; i++) {
System.out.println(a[i]);
}//for end
}//test4() end
public static void test5(int a, int b) {
System.out.println(a);
System.out.println(b);
}//test5() end
public static void test6(int[][] a) {
for(int i=0; i<a.length; i++) {
int col=a[i].length;
for(int j=0; j<col; j++) {
System.out.println(a[i][j]);
}//for end
}//for end
}//test6() end
public static void main(String[] args) {
int[] num= {10, 20, 30};
//Call by value 값에 의한 호출 방식
test1(num[0], num[2]); //10, 30
//Call by reference 주소에 의한 호출 방식
test2(num); //배열 요소가 저장되어 있는 주소
//배열 전체
String[] name= {"무궁화","오필승","코리아"};
test3(name[0], name[2]); //무궁화, 코리아
test4(name); //name 전체
//[2행][3열]
int[][] su= {
{1, 2, 3}
,{4, 5, 6}
};
test5(su[0][0], su[1][1]); //1, 5
test6(su);
//////////////////////////////////////
int[] lotto= {3, 7, 4, 15, 28, 13};
//1차원 배열을 전달하면 오름차순 정렬
Arrays.sort(lotto);
for(int i=0; i<lotto.length; i++) {
System.out.println(lotto[i]);
}//for end
}//main() end
}//class end
'Backend > JAVA' 카테고리의 다른 글
[JAVA] quiz: 배열-편차구하기 (0) | 2023.03.15 |
---|---|
[JAVA] overload (0) | 2023.03.15 |
[JAVA] Sort 정렬 (0) | 2023.03.14 |
[JAVA] Array (0) | 2023.03.14 |
[JAVA] quiz: 배열 연습문제 (1) | 2023.03.14 |