동아리

자바 2차시

@_@.... 2020. 9. 23. 23:12

if문

C언어와 형태가 같다.

public class Emotion{
	public static void main(string[] args){
    	int height = 191;
   		if (height >= 190) System.out.printIn("OO보다 키가 크네요.");
    	else if (height >= 180) System.out.printIn("OO보다 키가 매우 조금 작네요.");
    	else if (height >= 170) System.out.printIn("OO보다 키가 조금 작네요.");
        else System.out.printIn("OO보다 키가 많이 작네요");
    }
}
//결과 - OO보다 키가 크네요 출력

switch문

public class Emotion{
	public static void main(string[] args){
    	int age = 1;
        case 1: System.out.printIn("입학을 축하해요."); break;
        case 2 System.out.printIn("2 학 년"); break;
        case 3 System.out.printIn("정신나갈것같애"); break;
        default: System.out.printIn("고등학생 맞아요?"); break;
    }
}
// 입학을 축하해요 출력.

논리 연산자

&&(AND) / ! (NOT) / || (OR) 은 C언어와 형태가 같음.

AND

public class Emotion {
	public static void main(String[] args) {
    	if (true && true) System.out.printIn(1);
        if (true && false) System.out.printIn(2);
        if (false  && true) System.out.printIn(3);
        if (false && false) System.out.printIn(4);
    }
}

OR

public class Emotion {
	public static void main(String[] args) {
    	if (true || true) System.out.printIn(1);
        if (true || false) System.out.printIn(2);
        if (false || true) System.out.printIn(3);
        if (false || false) System.out.printIn(4);
    }
}

NOT

public class Emotion {
	public static void main(String[] args) {
    	if (!true) System.out.printIn(1);
        if (!false) System.out.printIn(2);
    }
}

while문

public class Emotion{
	public static void main(String[] args){
    	while(true){
        	System.out.printIn("이모션 사랑해요");
        }
    }
}
//while(true) = 무한루프

 

for문

public class Emotion{
	public static void main(String[] args){
    	for(int i=1; i<15; i++){
        	if(i%2==0)
            	continue;
            else
            	print(i);
                break;
        }
    }
}

배열

public class Emotion{
	public static void main(String[] args){
    	String[] asdf = {"ㅇㅇㅇ", "ㄷㄷㄷ","ㄱㄱㄱ"};
        for(int i=0; i<=3; i++) System.out.printIn(asdf[i]);
    }
}
/*
출력 결과
ㅇㅇㅇ
ㄷㄷㄷ
ㄱㄱㄱ
*/