Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- java변수
- While
- JAVA객체지향
- 메서드
- 반복문
- 시스템 환경 변수 편집
- function
- 메서드 오버로딩
- Java
- this예약어
- Thread
- 생성자
- IntelliJ IDEA
- 접근제어지시자
- 자바 멀티스레딩
- 형 변환
- 인텔리제이 기초 설정
- 연관관계
- for문
- OPP개념
- 인텔리제이 한글 깨짐 해결법
- 상수
- break문
- JAVA기초
- multi-threading
- 컴파일
- 포함관계
- 집합관계
- Java데이터 타입
- continue문
Archives
- Today
- Total
최원종의 개발 블로그
Java( 조건문) 본문
조건문은 프로그래밍에서 특정 조건을 평가하고, 그 결과에 따라 다른 실행 흐름을 따르도록 하는 구문.
조건문을 사용하면 주어진 조건이 참(true) 일 때와 거짓(false) 일 때 수행할 작업을 다르게 지정할 수 있다.
if 구문 단독
public class IfTest {
public static void main(String[] args) {
//조건문(제어문) - if
// 만약 ... 이라면
//if 문 단독, is else 구문, if else if else
boolean flag = true;
//if문 단독 -> 실행에 흐름을 제어한다(제어문)
if (!flag) {
//참 이면 구문이 동작
System.out.println("조건식이 true이면 실행이 된다");
}//end of if
System.out.println("코드가 여기까지 내려옵니다");
}//end of main
}// end of class
if - else문
public class IfElseTest {
//코드의 시작점
public static void main(String[] args) {
int age = 20;
//if else 구문 -> 반드시 하나는 수행이 된다
if (age >= 21) {
System.out.println("고등학교 졸업");
} else {
System.out.println("아직 급식 중");
}//end of if
}//end of main
}//end of class
if else if else문
public class IfElseIfElse {
//코드의 시작점
public static void main(String[] args) {
int age = 27;
if(age <= 7) {
System.out.println("학교에 아직 안다님");
} else if (age <= 13) {
System.out.println("초등학교에 다님");
} else if (age <= 16) {
System.out.println("중학교에 다님");
} else if (age <= 19) {
System.out.println("고등학교에 다님");
}else {
System.out.println("학교에 안다님 성인임");
}
//else 가 있기 때문에 반드시 한번은 수행이 된다
}//end of main
}// end of class
if문 연습 문제
package ch04;
import java.util.Scanner;
public class IfEx2 {
public static void main(String[] args) {
//요구사항
//사용자에게 정수 값(성적을 입력 받아서 학점을 출력하도록 코딩)
System.out.println("==성적을 입력하세요==");
Scanner sc = new Scanner(System.in);
int score = sc.nextInt();
System.out.println("로깅 - 받은 값 확인 : " + score);
//방어적 코드를 잘 써줘야 한다
//100~90
//120점 넣으면 잘못된 출력입니다
//0보다 작은 값이라면 ---> 잘못된 입력입니다
// 100보다 큰 값이면 --> 잘못된 입력입니다.
if (score < 0 || score > 100) {
System.out.println("잘못된 입력값입니다 0에서 100사이의 숫자를 입력하시오");
} else {
if (score >= 90) {
System.out.println("A학점");
} else if (score >= 80) {
System.out.println("B학점");
} else if (score >= 70) {
System.out.println("C학점");
} else if (score >= 60) {
System.out.println("D학점");
} else if (score >= 50) {
System.out.println("E학점");
} else {
System.out.println("F학점");
}//end of inner if
}//end of outer if
//if 구문 안에 또 다른 if 구문을 추가할 수 있다
//중쳡 if문이라고 한다
// if ((score <= 100) && (score >= 90)) {
// System.out.println("A학점");
// } else if ((score <= 89) && (score >= 80)) {
// System.out.println("B학점");
// } else if ((score <= 79) && (score >= 70)) {
// System.out.println("C학점");
// } else if ((score <= 69) && (score >= 60)) {
// System.out.println("D학점");
// } else if ((score <= 59) && (score >= 50)) {
// System.out.println("E학점");
// } else if ((score <= 49) && (score >= 0)) {
// System.out.println("F학점");
// } else {
// System.out.println("잘못 입력된 정보입니다.");
// }//end of immer if
//조건문을 완성하시오
//90점 이상이면 --> A학점 출력
//80점 이상이면 --> B학점 출력
//70점 이상이면 --> C학점 출력
//60점 이상이면 --> D학점 출력
//50점 이상이면 --> E학점 출력
//40점 이상이면 --> F학점 출력
}// end of main
}// end of class
'Java > 자바 기본' 카테고리의 다른 글
| while문 (0) | 2026.02.23 |
|---|---|
| Java(반복문) (0) | 2026.02.23 |
| Java(연산자) (1) | 2026.02.18 |
| Java(데이터 타입, 상수, 형 변환, 컴파일) (0) | 2026.02.12 |
| Java(자바 기초, 변수) (0) | 2026.02.11 |
