목차

1) 4대 제어문 형태

2) if 문

3) 삼항연산자


1. 4대 제어문 형태

 

		/* 4대 제어문 
		 
		 * 조건 if switch
		 * 반복 for while  
		 
		 * 기본 구조 
		 
		 if () {
		 	
		 }
		 
		 switch () {
		 
		 }
		 
		 for () {
		 
		 }
		 
		 while () {
		 
		 }
		 
		 */

2. if 문

 

package Pack;

public class Hello01 {

	public static void main(String[] args) {
		/* if */
		
		System.out.println("시작");
		if (true) {
			System.out.println("tiger");
			System.out.println("elephant");
			System.out.println("parrot");
		}
		System.out.println("11111111111111111111");
		
		if (false) {
			System.out.println("tiger");
			System.out.println("elephant");
			System.out.println("parrot");
		}
		System.out.println("22222222222222222222");
		
		if (3>2) {
			System.out.println("tiger");
			System.out.println("elephant");
			System.out.println("parrot");
		}System.out.println("333333333333333333333");
		
		if (3<2) {
			System.out.println("tiger");
			System.out.println("elephant");
			System.out.println("parrot");
		}System.out.println("444444444444444444444");
		
		if (3>2 && 7==7) {
			System.out.println("tiger");
			System.out.println("elephant");
			System.out.println("parrot");
		}System.out.println("555555555555555555555");
		
		if (3>2 && 7!=7) {
			System.out.println("tiger");
			System.out.println("elephant");
			System.out.println("parrot");
		}System.out.println("666666666666666666666");
		
		if (3>2 || 7==7) {
			System.out.println("tiger");
			System.out.println("elephant");
			System.out.println("parrot");
		}System.out.println("777777777777777777777");
		
		if (3>2 || 7!=7) {
			System.out.println("tiger");
			System.out.println("elephant");
			System.out.println("parrot");
		}System.out.println("888888888888888888888");
		
		System.out.println("끝");
		
		
		/*	출력 결과
        	시작
			tiger
			elephant
			parrot
			11111111111111111111
			22222222222222222222
			tiger
			elephant
			parrot
			333333333333333333333
			444444444444444444444
			tiger
			elephant
			parrot
			555555555555555555555
			666666666666666666666
			tiger
			elephant
			parrot
			777777777777777777777
			tiger
			elephant
			parrot
			888888888888888888888
			끝	*/
		
	}

}

 

예시

 

1-1 학점 계산기

package PACK01;

public class Hello01 {

	public static void main(String[] args) {
		int num = 95;
		if (num>=90) {
			System.out.println("학점은 A입니다.");
		}else if (num>=80) {
			System.out.println("학점은 B입니다.");
		}else if (num>=70) {
			System.out.println("학점은 C입니다.");
		}else if (num>=60) {
			System.out.println("학점은 D입니다.");
		}else {
			System.out.println("재수강입니다.");
		}
	}

}

1-2 띠 구하기

package PACK01;

public class Hello01 {

	public static void main(String[] args) {
		int year = 1545;
		int q = year % 12;
		
		if(q==0) {
			System.out.println("원숭이 띠(신)");
		}else if (q==1) {
			System.out.println("닭 띠(유)");
		}else if (q==2) {
			System.out.println("개 띠(술)");
		}else if (q==3) {
			System.out.println("돼지 띠(해)");
		}else if (q==3) {
			System.out.println("쥐띠 (자)");
		}else if (q==3) {
			System.out.println("소 띠(축)");
		}else if (q==3) {
			System.out.println("범 띠(인)");
		}else if (q==3) {
			System.out.println("토끼 띠(묘)");
		}else if (q==3) {
			System.out.println("용 띠(진)");
		}else if (q==3) {
			System.out.println("뱀 띠(사)");
		}else if (q==3) {
			System.out.println("말 띠(오)");
		}else if (q==3) {
			System.out.println("양 띠(미)");
		}
	}

}

1-3 짝수 홀수

package PACK01;

public class Hello01 {

	public static void main(String[] args) {
		/*중첩 if문*/
		System.out.println("시작");
		if(true) {
			System.out.println("호랑이1");
			if(3>2) {
				System.out.println("코끼리");
			}System.out.println("호랑이 2");
		}
		
		int num = 29;
		int q = num/10; //십의 자리 숫자 
		int r = num%10;
		System.out.println(q);
		System.out.println(r);
		
		if(q%2==0) {
			System.out.println("십의자리는 짝수이고,");
			if(r%2==0) {
				System.out.println("일의자리는 짝수이다.");
			}else {
				System.out.println("일의 자리는 홀수이다.");
			}
		}else {
			System.out.println("십의 자리는 홀수이고");
			if(r%2==0) {
				System.out.println("일의자리는 짝수이다.");
			}else {
				System.out.println("일의자리는 홀수이다.");
			}
		}
	}
}

3. 삼항연산자

package PACK01;
import java.util.Random;
import java.util.Scanner;

public class Hello01 {

	public static void main(String[] args) {
		/* 삼항 연산자 */
		int a;
		
		//조건이 걸리면서 어떤 쪽이든 변수가 값을 받을 때, 삼항연산으로 바꾼다.
		if(3>2) {
			a = 10;
		}else {
			a=20;
		}
		System.out.println(a); //10
		
		a = (3>2) ? 10 : 20;
		System.out.println(a); //
		//a = (조건) ? 만족할때 : 아닐때
		
	}
}

예제3.

+ Recent posts