JAVA/10_method
char타입 리턴
Y_____527
2021. 1. 18. 21:49
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
package ex01.method;
public class Ex02_Method {
//==========char타입===========
public static char disp(int jumsu) { //함수정의부(구현부)
char grade = ' ';
switch (jumsu / 10){
case 10:
case 9: grade='A'; break;
case 8: grade='B'; break;
case 7: grade='C'; break;
case 6: grade='D'; break;
default: grade = 'F'; break;
} // end switch
return grade;
} //disp() end
//<<<<<<<<<<<<<<메인>>>>>>>>>>>>>>>
public static void main(String[] args) {
System.out.println("grade = " + disp(99) + "학점");
char ch = disp(55); //함수호출
System.out.println("grade = " + ch + "학점");
System.out.println("-----------------------------");
//==========main에서 설계함수를 미리 쓴 후->관련 함수 작성(자동완성가능)=========
sub(10,20);
}
public static void sub(int x, int y) {
//항상 양수 결과값만 출력하기
//방법1-if else
if(x>y) System.out.println(x + "-" + y + "=" + (x-y));
else System.out.println(y + "-" + x + "=" + (y-x));
//방법2-삼항연산자
int result = 0;
result = (x<y) ? (y-x):(x-y);
System.out.println("result = " + result);
//cf)함수에서 다른함수 호출도 가능
System.out.println("sub에서 호출했어요 ==> " + disp(77) + "학점");
}
}
|
cs |
grade = A학점
grade = F학점
-----------------------------
20-10=10
result = 10
sub에서 호출했어요 ==> C학점
▶main에서 설계함수를 미리 쓴 후->관련 함수 작성(자동완성가능)