boolean result = (5 > 3) && (3 < 5); // AND
result = (5 > 3) || (3 < 2); // OR
result = !(5 > 3); // NOT
4. 제어문
조건문
java
if (condition) {
// 조건이 참일 때 실행
} else if (anotherCondition) {
// 다른 조건이 참일 때 실행
} else {
// 모든 조건이 거짓일 때 실행
}
switch문
java
int number = 2;
switch (number) {
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;
default:
System.out.println("Not one or two");
}
반복문
for문
java
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
while문
java
int i = 0;
while (i < 10) {
System.out.println(i);
i++;
}
do-while문
java
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 10);
5. 배열
java
int[] numbers = {1, 2, 3, 4, 5};
for (int number : numbers) {
System.out.println(number);
}
6. 객체 지향 프로그래밍
클래스 및 객체 생성
java
public class Car {
// 필드
private String color;
private String model;
// 생성자
public Car(String color, String model) {
this.color = color;
this.model = model;
}
// 메소드
public void displayInfo() {
System.out.println("Car model: " + model + ", color: " + color);
}
}
객체 사용
java
Car myCar = new Car("Red", "Toyota");
myCar.displayInfo();
상속
java
public class ElectricCar extends Car {
private int batteryLife;
public ElectricCar(String color, String model, int batteryLife) {
super(color, model);
this.batteryLife = batteryLife;
}
}
인터페이스
java
public interface Drivable {
void drive();
}
public class Bicycle implements Drivable {
public void drive() {
System.out.println("Bicycle is being driven");
}
}