if_else 조건 1 

 

package Ifmoon;

public class Basic_if_else {

	// if문의 옵션 2종류
//		1. else
//			- 조건이 2가지인 경우 else를 이용
//		2. else if 
			
	
	public static void main(String[] args) {
		 int num = 1;
		 
		 if(num % 2 == 0) {
			 System.out.println("짝수");
		 }
		 if(num % 2 == 1) {
			 System.out.println("홀수");
		 }
		 System.out.println("-------------");
		 
		 if(num % 2 == 0) {
			 System.out.println("짝수");
		 }else {
			 // if의 조건이 아니면 무조건 실행
			System.out.println("홀수");
		}
	}

}


if_else 조건 2

 

package Ifmoon;

public class Basic_if_else {

			
	
	public static void main(String[] args) {
		int select = 1;// 번호 입력
		
		System.out.println(">> 자판기 <<");
		System.out.println("[1.과자 2.음료수 3.라면]");

		System.out.println("번호를 입력하세요 : " + select );
		
		if(select == 1) {System.out.println("1.과자");}
		if(select == 2) {System.out.println("2.음료수");}
		if(select == 3) {System.out.println("3.라면");}
		else { System.out.println("오류"); }
		//else는 바로 위에 있는 if문이랑만 연결 되있음
		//바로 위에 있는 if문이 false이므로 "오류" 출력됨
		
		System.out.println("---------------");
		
		System.out.println(">> 자판기 <<");
		System.out.println("[1.과자 2.음료수 3.라면]");
		select = 1;
		if(select == 1) {System.out.println("1.과자");}
		if(select == 2) {System.out.println("2.음료수");}
		if(select == 3) {System.out.println("3.라면");}
		if(3 < select || select < 1) { System.out.println("오류"); }
		// 1 ~ 3 숫자가 아닌 경우 오류
	}

}

 

 


 

else if 조건

package Ifmoon;

public class Basic_if_else {
/*
	if문의 옵션 else if 
	1. if문 가지고도 정확한 코드를 작성할 수 있지만,
	   else if를 사용하면 조건문의 조건이 여러개인 경우
	   보다 호율적으로 코드를 작성할 수 있다.
	2. 구조
		if(조건식1) {
			조건식1이 참(true)일 때, 실행할 문장;
		}else if(조건식2){
			조건식2가 참(true)일 때, 실행할 문장;
		}else if(조건식3){
			조건식3이 참(true)일 때, 실행할 문장;
		}else {
			위 조건을 모두 만족하지 않을 때, 실행할 문장;
		}
	
										*/
	
	public static void main(String[] args) {
		
		// if에서 else if 2개를 거쳐
		System.out.println("-----------------");
		System.out.println(">>> 과일");
		System.out.println("[1.사과 2.포도 3.감]");
		System.out.println("번호를 입력하세요 : 5");
		
		int selsect = 5;
		if(selsect == 1) {System.out.println("1.사과");}
		else if(selsect == 2) {System.out.println("2.포도");}
		else if(selsect == 3) {System.out.println("3.감");}
		else {System.out.println("오류1");}
		//위에 전부 false이기 때문에 "오류" 출력
		
		System.out.println("-----------------");
		
	
		
	}

}

 

 

+ Recent posts