반응형
Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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
Tags
more
Archives
Today
Total
관리 메뉴

비전공개미 개발노트

Java 2일차 - 제어문, 메소드 본문

프로그래밍/Java

Java 2일차 - 제어문, 메소드

비전공개미 2022. 9. 19. 17:40
반응형
SMALL

정수 int age = 0, hap = 0;
실수 double avg = 0.0, float weight = 57.9f;
문자 char grade = 'F';
불    boolean flag = true, gender = false;
문자열 String title = "게시판", juso = "제주";

연산 - 사칙, 관계, 논리, 비트, 단항, 삼항, 대입연산자

문제] 
int kor = 90, eng = 85, hap = 0;
hap = kor + eng;
double avg = hap / 2;
//평균점수 70점부터 축합격, 0~69 재시험
String msg;

if(avg >= 70){
	msg = "축합격";
}else{
	msg = "재시험";
}
System.out.println(msg);
//출력
축합격

 

삼항연산
//삼항연산은 변수로 값을 받아야한다.
String result = (avg >= 70) ? "축합격" : "재시험";
System.out.println(result);
//출력
축합격


제어 - if, switch
보조제어문 - break반복문탈출, switch탈출 / continue반복문복귀

[ if문 ]
if( 조건 ){ 처리; }

if( 조건 ){ 
    조건 충족시처리; 
}else{ 
    조건 미 충족시처리; 
}

if( 조건A ){ 
    조건A 충족시처리; 
}else if( 조건B ){ 
    조건B 충족시처리; 
}else{ 
    조건A/B 모두 미 충족시 처리; 
}

[ switch문 ]
switch( 조건 ){ 
    case A값: 처리; break;
    case B값: 처리; break;
    case C값: 처리; break;
    default: 기타처리; break;
}


반복문 
for( 시작값1; 최종조건2; 증감3 ){ 처리4 }
    1 2 4 3 순서로 처리됨
while( true ){ 반드시 if조건으로 break처리 }
do{ 최소1회 처리; }while( 조건 );


method = 메소드 = 함수 = 처리 = 프로시저 = function = def

1. 리턴값

   public static 리턴값 함수이름( ) { 구현기술... }

   리턴값이 없이 단순히 처리만 void
   ㄴpublic static  함수이름( ) { 구현기술...  return true; }

   리턴값이 있는 String, double, int, boolean
   ㄴpublic static int 함수이름( ) { 구현기술...  return 정수값; }
   ㄴpublic static String 함수이름( ) { 구현기술...  return "board"; }
   ㄴpublic static boolean 함수이름( ) { 구현기술...  return true; }
   ㄴpublic static double 함수이름( ) { 구현기술...  return 78.9; }

 

2. 매개인자는 함수이름( ~~~ )

   public static 리턴값 함수이름( String a, int b ) { 구현기술... }
   public static 리턴값 함수이름( int a, int b ) { 구현기술... }
   public static 리턴값 함수이름( int a, double b ) { 구현기술... }
   public static 리턴값 함수이름( boolean a ) { 구현기술... }
   public static 리턴값 함수이름( int a, int b, int c ) { 구현기술... }
   public static 리턴값 함수이름( String a[ ] ) { 구현기술... }
   public static 리턴값 함수이름( int a[ ], String str ) { 구현기술... }

   public static void test1( String name, int age ) {  }
   public static void test2( int a, int b ) {  }
   public static void test3( int a, double b ) {  }
   public static void test4( boolean a ) {  }
   public static void test5( int a, int b, int c ) {  }
   public static void test6( String a[ ] ) {  }
   public static void test7( int a[ ], String str ) {  }

public static void main(String[] args) {
    test1( "hong", 31 );
    test2( 70, 90 );
    test3( 90, 45.6 );
    test4( false );
    test5( 7, 8, 9 );
    test6( 배열타입 );
    test7( 배열, "월요일" );
}

 

3. static키워드 + 리턴값 / 매개인자

   메인함수 = 메모리진입 main함수영역 static = 상주하는 영역

//ex1) 에러없음(정상)
public class Aws {
    public static void main(String[] args) { test("길동", 27); }
    public static void test(String name, int age) { }
}

//ex2) 에러발생
public class Aws {
    public static void main(String[] args) { test("길동", 27); }
    public void test(String name, int age) { }
}

//해결방법
public class Aws {
    public static void main(String[] args) { 
        Aws my = new Aws( ); //new키워드 메모리영역 heap=실행중에 할당
        my.test("길동", 27);
    }
    public void test(String name, int age) { }
}
반응형
LIST
Comments