JAVA

조건문 (if), Switch, Math클래스

연93 2022. 9. 6. 11:58

if else문

	public static void main(String[] args) {


		int score = 75;
		
		// 첫번째 조건식에서 상위 조건을 적용해야 다음 조건식들은 순차적으로 해야 한다.
		if(score >= 90) {
			System.out.println("등급A");
		}else if(score >= 80) {
			System.out.println("등급B");
		}else if(score >= 70) {
			System.out.println("등급C");
		}else {
			System.out.println("등급D");
		}

	}

}

Math클래스

// Math클래스 : 수학관련기능을 제공하는 클래스.
		// 0.0 이상~ 20.0미만 -> 0이상에서 20미만 -> 81이상에서 101미만
		//System.out.println((int)(Math.random() * 20) + 81);
		
		// 80이상100이하의 점수가 랜덤으로 생성. 특정범위의 값을 추출
		int score = (int)(Math.random() * 20) + 81;
		System.out.println("점수: " + score);

Switch

	public static void main(String[] args) {
		
		char grade = 'B';
		
		// 변수의 데이터타입이 정수형데이터타입, 문자열, 열거형...
		switch(grade) {
			case 'A':
			case 'a':
				System.out.println("우수회원");
				break;
			case 'B':
			case 'b':
				System.out.println("일반회원");
				break;
			default:
				System.out.println("손님");
				
		}

	}

}
-------------------------------------------------------
	public static void main(String[] args) {
		
		String position = "과장";
		
		switch(position) {
			case "부장":
				System.out.println("700만원");
				break;
			case "과장":
				System.out.println("3500만원");
				break;
			case "사원":
				System.out.println("3000만원");
				break;
			default: // default 생략가능
				System.out.println("손님입니다");
		}

	}

}