✍매개변수 x, 리턴값 x 메서드
public class Hello {
public static void main(String[] args) {
System.out.println("1. 매개변수x, 리턴값x 메서드");
hello();
}
public static void hello() {
for(int i=0; i<10; i++) {
System.out.println("Hello, World!");
}
}
}
- 리턴값이 없기 때문에 void사용
💻
✍매개변수 o, 리턴값 x 메서드
public class Hello {
public static void main(String[] args) {
System.out.println("2. 매개변수o, 리턴값x 메서드");
hello("안녕하세요.", 5);
}
public static void hello(String str, int count) {
for(int i=0; i<count; i++) {
System.out.println(str);
}
}
}
- 리턴값이 없는 메서드이기 때문에 void타입을 사용
- 매개변수의 타입을 메서드에 명시해주었다.
💻
- 메인메서드에서 "안녕하세요." 문자열과, int를 호출하였다.
✍매개변수 x, 리턴값 o 메서드
public class Hello {
public static void main(String[] args) {
System.out.println("3. 매개변수x, 리턴값o 메서드");
String result = hello();
System.out.println(result);
}
public static String hello() {
return "안녕하세요.";
}
}
- 리턴값이 존재하기 때문에 리턴타입 String을 적어주었다.
💻
✍매개변수 o, 리턴값 o 메서드
public class test1 {
public static void main(String[] args) {
String result = hello(0);
System.out.println("용돈을 받아서 " + result);
}
public static String hello(int money) {
money += 5000;
System.out.println("용돈을 " + money +"원 더 받았다");
return "기쁘다";
}
}
- 리턴값이 존재하기 때문에 리턴타입 String을 지정하였다.
- 매개변수 int를 메서드에 명시하였다.
💻
'개발 > Java' 카테고리의 다른 글
[Java] UP&DOWN 게임 만들기 (0) | 2023.01.07 |
---|---|
[Java] 반복문을 사용하여 간단한 3, 6, 9 게임 만들기 (0) | 2023.01.07 |
[Java] 메서드(Method) (0) | 2023.01.05 |
[Java] break & continue (0) | 2023.01.05 |
[Java] for문 별 찍기 (0) | 2023.01.04 |