코드를 재사용하기 위해서
public class Main {
static void myMethod() {
System.out.println("선언된 메서드를 호출하면 출력");
}
public static void main(String[] args) {
myMethod();
}
}
// 선언된 메서드를 호출하면 출력
public class Main {
static void myMethod() {
System.out.println("선언된 메서드를 호출하면 출력");
}
public static void main(String[] args) {
myMethod();
myMethod();
myMethod();
}
}
// 선언된 메서드를 호출하면 출력
// 선언된 메서드를 호출하면 출력
// 선언된 메서드를 호출하면 출력
public class Main {
static void myMethod(String fname) {
System.out.println(fname + " Lee");
}
public static void main(String[] args) {
myMethod("Seonga");
myMethod("Jeonga");
myMethod("Cheonga");
}
}
// Seonga Lee
// Jeonga Lee
// Cheonga Lee
public class Main {
static void myMethod(String fname, int age) {
System.out.println(fname + " is " + age);
}
public static void main(String[] args) {
myMethod("Seonga", 25);
myMethod("Jeonga", 24);
myMethod("Cheonga", 23);
}
}
// Seonga is 25
// Jeonga is 24
// Cheonga is 23
public class Main {
// Create a checkAge() method with an integer variable called age
static void checkAge(int age) {
// If age is less than 18, print "access denied"
if (age < 18) {
System.out.println("Access denied - You are not old enough!");
// If age is greater than, or equal to, 18, print "access granted"
} else {
System.out.println("Access granted - You are old enough!");
}
}
public static void main(String[] args) {
checkAge(20); // Call the checkAge method and pass along an age of 20
}
}
// Outputs "Access granted - You are old enough!"
void 를 사용int, char 등)과 메서드 내부에 return을 사용public class Main {
static int myMethod(int x, int y) {
return x + y;
}
public static void main(String[] args) {
System.out.println(myMethod(5, 3));
}
}
// 8
public class Main {
static int myMethod(int x, int y) {
return x + y;
}
public static void main(String[] args) {
int z = myMethod(5, 3);
System.out.println(z);
}
}
// 8
static int plusMethod(int x, int y) {
return x + y;
}
static double plusMethod(double x, double y) {
return x + y;
}
public static void main(String[] args) {
int myNum1 = plusMethod(8, 5);
double myNum2 = plusMethod(4.3, 6.26);
System.out.println("int: " + myNum1);
System.out.println("double: " + myNum2);
}
// int:13
// double:10.56
public class Main {
public static void main(String[] args) {
// Code here CANNOT use x
int x = 100;
// Code here can use x
System.out.println(x);
}
}
// 100
{}public class Main {
public static void main(String[] args) {
// Code here CANNOT use x
{ // This is a block
// Code here CANNOT use x
int x = 100;
// Code here CAN use x
System.out.println(x);
} // The block ends here
// Code here CANNOT use x
}
}
// 100
public class Main {
public static void main(String[] args) {
int result = sum(10);
System.out.println(result);
} } } }
public static int sum(int k) {
if (k > 0) {
return k + sum(k - 1);
} else {
return 0;
// 55
모든 재귀 함수는 중지 조건이 있어야 하며,
이는 함수가 자기 자신을 호출하는 것을 멈추는 조건
무한 재귀는 함수가 자기 자신을 호출하는 것을 멈추지 않는 경우
재귀를 사용하여 5에서 10까지의 모든 숫자 합산
public class Main {
public static void main(String[] args) {
int result = sum(5, 10);
System.out.println(result);
}
public static int sum(int start, int end) {
if (end > start) {
return end + sum(start, end - 1);
} else {
return end;
}
}
}
출처 : W3Schools Online Web Tutorials