Spring & Java
JAVA 문법 (1)조건문 본문
● 조건문 - 프로그램의 선택지
● 메서드 - 모듈화의 시작
package Study4;
// 조건문을 왜 배워야 할까요?
// ● 조건문 - 프로그램의 선택지
// 조건문의 구조
// if ( 조건 수식 ) {
// 명령문; // 실행 할까 말까?
// }
// 학습 키워드
// if - 조건 수식이 참일 때 실행
// if - else - 조건 수식이 거짓일 때 실행
// else - if - 조건 여러개 일 경우 사용 한다.
// switch - 특정 값에 따라 여러 동작을 실행
// if문
// 여러개 조건을 줄때는 else if (){}
//
public class Main {
public static void main(String[] args) {
// String light = "빨간불";
//
// if (light.equals("초록불")) {
// System.out.println("건너세요!");
// } else {
// System.out.println("멈추세요!");
// }
// System.out.println("...");
// switch 문
// switch 문은 값에 따라 여러 동작을 실행
// ● 괄호안에 단일값만 들어갈 수 있습니다. 조건식은 사용할 수 없습니다.
int number = 1;
switch (number){
case 1:
System.out.println("1입니다.");
break;
case 2:
System.out.println("2입니다.");
break; //break >> 탈출 버튼 // 꼭 중요한건 case 문 끝에 항상 break 사용 해야 된다.
//fall-through 현상이 생기기 때문에 break 꼭 사용해야 된다.
default:
System.out.println("1도 아니고 2도 아닙니다.");
}
}
}
package Study4;
//코딩 실력을 빠르게 향상시키는 방법
//언어 실력을 빠르게 키우려면 직접 생각하며 외국인과 대화하는 것이 효과적이듯 코딩 실력도 직접 코드를 작성하고 실행해보는 경험이 많을수록 향상됩니다.
//손으로 직접 타이핑하고 실행하는 과정이 반복될수록 개념이 더 깊이 이해되고 실전에서 활용하는 능력이 자연스럽게 길러집니다.
//Q1. 신호등 색상을 입력하면 프로그램이 올바른 안내 메시지를 출력하도록 만드세요!
// 새로운 클래스(TrafficLight.java) 를 만들어서 진행하세요.
import java.util.Scanner;
//// 예상 출력:
//신호등 색상을 입력하세요 (초록불, 노란불, 빨간불): 초록불
//건너세요!
//신호등 색상을 입력하세요 (초록불, 노란불, 빨간불): 노란불
//주의하세요!
//신호등 색상을 입력하세요 (초록불, 노란불, 빨간불): 빨간불
//멈추세요!
//신호등 색상을 입력하세요 (초록불, 노란불, 빨간불): 파란불
//잘못된 입력입니다.
public class TestIf {
static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("신호등 색상을 입력 하세요( 초록불/노란불/빨간불, 종료: exit )") ;
String textbox = sc.nextLine();
if (textbox.equals("exit")) {
System.out.println("프로그램을 종료 합니다.");
break;
}
switch (textbox) {
case "초록불":
System.out.println("초록불 입니다.");
break;
case "노란불":
System.out.println(" 서행 후 멈추세요!");
break;
case "빨간불":
System.out.println("완벽히 정차 하세요!");
break;
default:
System.out.println("잘못된 신호 방식 입니다.");
}
}
}
}
< QUIZ >
기본 if문
- int a = 5; if (a > 3) System.out.println("크다"); → 출력 결과는? true 크다. ㅇ
- int a = 2; if (a > 3) System.out.println("크다"); → 출력 결과는? false 작다 ㅇ 출력 x
- int a = 10; if (a == 10) System.out.println("같다"); → 출력 결과는? true 같다. ㅇ
- int a = 7; if (a != 7) System.out.println("다르다"); → 출력 결과는? false 다르다 ㅇ : 출력 x
- int a = 8; if (a >= 8) System.out.println("합격"); → 출력 결과는? true 합격 ㅇ
- int a = 9; if (a < 5) System.out.println("낮음"); else System.out.println("높음"); → 출력 결과는? false : 높음 ㅇ
- int a = 3; if (a > 5) System.out.println("A"); else System.out.println("B"); → 출력 결과는? B false ㅇ
- int a = 10; if (a % 2 == 0) System.out.println("짝수"); → 결과는? true 짝수 ㅇ
- int a = 7; if (a % 2 == 1) System.out.println("홀수"); → 결과는? true 홀수 ㅇ
- int a = 10; if (a % 5 == 0) System.out.println("5의 배수"); → 결과는? true 5의 배수 ㅇ
✅ if-else 문
- int a = 5; if (a > 10) System.out.println("크다"); else System.out.println("작다"); → 결과는? 작다 ㅇ
- int a = 15; if (a > 10) System.out.println("크다"); else System.out.println("작다"); → 결과는? 크다 ㅇ
- int score = 85; if (score >= 90) System.out.println("A"); else System.out.println("B"); → 결과는? B ㅇ
- int score = 92; if (score >= 90) System.out.println("A"); else System.out.println("B"); → 결과는? A ㅇ
- int num = -1; if (num > 0) System.out.println("양수"); else System.out.println("음수"); → 결과는? 음수 ㅇ
- int num = 0; if (num > 0) System.out.println("양수"); else System.out.println("음수"); → 결과는? 출력없음 X
- int x = 7; if (x % 2 == 0) System.out.println("짝수"); else System.out.println("홀수"); → 결과는? 홀수 ㅇ
- int x = 12; if (x % 3 == 0) System.out.println("3의 배수"); else System.out.println("아님"); → 결과는? 아님 X
- int x = 13; if (x % 3 == 0) System.out.println("3의 배수"); else System.out.println("아님"); → 결과는? 3의 배수 ㅇ
- int x = 15; if (x % 5 == 0) System.out.println("O"); else System.out.println("X"); → 결과는? O ㅇ
✅ if - else if - else 구조
- int score = 95; if(score >= 90) System.out.println("A"); else if(score >= 80) System.out.println("B"); else System.out.println("C"); → 결과는? A ㅇ
- int score = 83; … → 결과는? B ㅇ
- int score = 75; … → 결과는? C ㅇ
- int score = 59; … → 결과는? C ㅇ
- int score = 89; … → 결과는? B ㅇ
- int age = 19; if(age >= 20) System.out.println("성인"); else System.out.println("미성년자"); → 결과는? 미성년자 ㅇ
- int temp = 25; if(temp > 30) System.out.println("더움"); else if(temp >= 20) System.out.println("적당"); else System.out.println("추움"); → 결과는? 적당 ㅇ
- int temp = 10; → 결과는? 추움 ㅇ
- int temp = 33; → 결과는? 더움 ㅇ
- int hour = 8; if(hour < 12) System.out.println("오전"); else System.out.println("오후"); → 결과는? 오전 ㅇ
✅ 논리 연산자 포함
- int a = 5; if(a > 0 && a < 10) System.out.println("한 자리수"); → 결과는? (참) (참) " 한자리수 " ㅇ
- int a = 15; if(a > 0 && a < 10) System.out.println("한 자리수"); → 결과는? (참) (거짓) " 출력 없음 " ㅇ
- int n = 7; if(n % 2 == 1 && n < 10) System.out.println("홀수 한 자리"); → 결과는? (참) (참) "홀수 한 자리" ㅇ
- int n = 12; if(n % 2 == 1 || n > 10) System.out.println("조건 만족"); → 결과는? (거짓) (참) "조건 만족" ㅇ
- int n = 8; if(!(n > 10)) System.out.println("10이하"); → 결과는? true "10이하" ㅇ
- int x = 10; if(x == 10 || x == 20) System.out.println("10또는20"); → 결과는? "10또는20" true ㅇ
- int x = 5; if(x == 10 || x == 20) System.out.println("10또는20"); else System.out.println("다름"); → 결과는? "다름" false ㅇ
- int x = 4, y = 5; if(x < y && y < 10) System.out.println("둘 다 참"); → 결과는? " 둘다 참" true ㅇ
- int x = 4, y = 15; if(x < y && y < 10) System.out.println("둘 다 참"); else System.out.println("거짓"); → 결과는? flase "거짓" ㅇ
- int a = 5; if(!(a == 5)) System.out.println("X"); else System.out.println("O"); → 결과는? "O" false ㅇ
✅ 중첩 if문
- int a = 10; if(a > 5) { if(a < 15) System.out.println("OK"); } → 결과는?
- int a = 20; if(a > 5) { if(a < 15) System.out.println("OK"); else System.out.println("NO"); } → 결과는?
- int score = 85; if(score >= 60) { if(score >= 90) System.out.println("A"); else System.out.println("B"); } → 결과는?
- int score = 55; if(score >= 60) { System.out.println("합격"); } else { System.out.println("불합격"); } → 결과는?
- int num = 0; if(num == 0) { System.out.println("Zero"); } else if(num > 0) { System.out.println("양수"); } else { System.out.println("음수"); } → 결과는?
- int num = -5; → 결과는?
- int num = 10; → 결과는?
- int n = 6; if(n % 2 == 0) { if(n % 3 == 0) System.out.println("2와3의 배수"); else System.out.println("2의 배수"); } → 결과는?
- int n = 9; … → 결과는?
- int n = 12; … → 결과는?
'JAVA 기초 문법 다지기' 카테고리의 다른 글
| Class 와 객체 (1) | 2025.12.05 |
|---|---|
| JAVA 배열 (3) (1) | 2025.12.04 |
| JAVA 반복문 - 자동화의 첫걸음 (2) (0) | 2025.12.04 |
| JAVA 문법 변수,연산자 (0) | 2025.12.03 |
| 자바 예습 기초 학습 (0) | 2025.11.28 |