제어문 예제 실행
s0401 ~ s0404 실행 및 분석
Sample0401_02: if 문, else 문이 사용되었다.Sample0401_03: if 문, else 문이 사용되었다.Sample0401_04: if 문, else 문이 사용되었다.Sample0401_05: if 문, else 문이 사용되었다.Sample0401_06: if 문, else if 문, else 문이 사용되었다.Sample0402_01: switch 문이 사용되었다.Sample0403_01: while 문이 사용되었다.Sample0403_02: while 문이 사용되었다.Sample0403_03: while 문, if 문이 사용되었다.Sample0403_04: while 문, if 문이 사용되었다.Sample0404_01: for 문이 사용되었다.Sample0404_02: for 문, if 문, else 문이 사용되었다.Sample0404_03: for 문, if 문이 사용되었다.Sample0404_04: for 문이 사용되었다.
조건문 연습
점수 → 학점, 숫자 → 요일
다음은 조건에 따라 점수를 학점으로 변환하는 예제다.
import java.util.Scanner;
public class IfPractice {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String result = "";
System.out.print("점수: ");
long score = sc.nextLong();
if (score >= 90) {
result = "A";
} else if (score >= 80) {
result = "B";
} else if (score >= 70) {
result = "C";
} else if (score >= 60) {
result = "D";
} else {
result = "F";
}
System.out.println(result);
sc.close();
}
}다음은 요일 번호(1~7)를 입력 받아 요일 이름을 출력하는 예제다.
import java.util.Scanner;
public class SwitchPractice {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String result = "";
System.out.print("요일 번호: ");
int id = sc.nextInt();
switch (id) {
case 1:
result = "월";
break;
case 2:
result = "화";
break;
case 3:
result = "수";
break;
case 4:
result = "목";
break;
case 5:
result = "금";
break;
case 6:
result = "토";
break;
case 7:
result = "일";
break;
default:
result = "알 수 없음";
}
System.out.println(result);
sc.close();
}
}반복문 연습
합계/팩토리얼/구구단 등 구현
public class IteratePractice {
public static void main(String[] args) {
// 1. 합계
long sum = 0;
long oddSum = 0;
long evenSum = 0;
for (int i = 0; i < 100; i += 1) {
if (i % 2 == 1) {
oddSum += i;
} else {
evenSum += i;
}
sum += i;
}
System.out.printf("전체 합계: %d\n", sum); // 4950
System.out.printf("홀수 합계: %d\n", oddSum); // 2500
System.out.printf("짝수 합계: %d\n", evenSum); // 2450
// 2. 팩토리얼
long result = 1;
long n = 5;
for (int i = 1; i <= n; i += 1) {
result *= i;
}
System.out.println(result); // 120
// 3. 구구단
for (int i = 1; i <= 9; i += 1) {
for (int j = 1; j <= 9; j += 1) {
System.out.printf("%d ", i * j);
}
System.out.println();
}
// 1 2 3 4 5 6 7 8 9
// 2 4 6 8 10 12 14 16 18
// 3 6 9 12 15 18 21 24 27
// 4 8 12 16 20 24 28 32 36
// 5 10 15 20 25 30 35 40 45
// 6 12 18 24 30 36 42 48 54
// 7 14 21 28 35 42 49 56 63
// 8 16 24 32 40 48 56 64 72
// 9 18 27 36 45 54 63 72 81
}
}메뉴 설계, 메뉴 루프 구현, 기능 분기 구현
설명
- 콘솔 메뉴 항목 설계
- while/do-while 로 반복 구조 구현
- if/switch 로 기능 분기
import java.util.ArrayList;
import java.util.Scanner;
public class TodoPractice {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("--------------------");
System.out.println(" TODO");
System.out.println("--------------------");
ArrayList<String> todos = new ArrayList<>();
while (true) {
printMenus();
System.out.print("메뉴 번호를 입력하세요: ");
int menuId = sc.nextInt();
System.out.println();
switch (menuId) {
case 1:
printTodos(todos);
break;
case 2:
addTodo(todos, sc);
break;
case 3:
removeTodo(todos, sc);
break;
case 4:
System.out.println("TODO 프로그램을 종료합니다.");
return;
default:
System.out.println("잘못된 메뉴 번호입니다.");
}
}
}
static void printMenus() {
System.out.println();
System.out.println("1. 조회");
System.out.println("2. 추가");
System.out.println("3. 삭제");
System.out.println("4. 종료");
System.out.println();
}
static void printTodos(ArrayList<String> todos) {
if (todos.isEmpty()) {
System.out.println("할 일 목록이 비어있습니다.");
return;
}
for (int i = 0; i < todos.size(); i += 1) {
System.out.printf("#%d %s\n", i + 1, todos.get(i));
}
}
static void addTodo(ArrayList<String> todos, Scanner sc) {
System.out.print("할 일: ");
sc.nextLine(); // 버퍼에 남은 개행 문자를 제거한다.
String todo = sc.nextLine();
todos.add(todo);
System.out.println();
System.out.println("할 일이 추가되었습니다.");
}
static void removeTodo(ArrayList<String> todos, Scanner sc) {
printTodos(todos);
System.out.println();
System.out.print("삭제할 할 일 번호: ");
int todoIndex = sc.nextInt() - 1;
if (todoIndex < 0 || todoIndex > todos.size() - 1) {
System.out.println();
System.out.println("잘못된 할 일 번호입니다.");
} else {
todos.remove(todoIndex);
}
}
}