-
Day 1 자바 기본 문법 학습코딩 테스트 2024. 12. 19. 20:06
1. 자바 기초 문법
1-1. 변수와 자료형
* 변수: 데이터를 저장하는 공간.
* 자료형: 변수의 데이터 타입을 나타냄.
자료형 크기 예시 int 4 bytes 정수형 (1, 2, 100) double 8 bytes 실수형 (3.14, 2.71) char 2 bytes 문자 ('A', 'b') String N/A 문자열 ("Hello") boolean 1 bit 논리형 (true/false) (예제: 변수 선언과 초기화)
int age = 25; // 정수형 변수 double pi = 3.14; // 실수형 변수 char initial = 'J'; // 문자형 변수 String name = "Java"; // 문자열 변수 boolean isTrue = true; // 논리형 변수
1-2. 조건문
- 조건에 따라 실행 흐름을 제어하는 구조이다.
* if-else 조건문
int score = 85; if (score >= 90) { System.out.println("A학점"); } else if (score >= 80) { System.out.println("B학점"); } else { System.out.println("C학점"); }
* switch 문
- 특정 값에 따라 여러 경로 중 하나를 선택.
int month = 3; switch (month) { case 1: System.out.println("January"); break; case 2: System.out.println("February"); break; case 3: System.out.println("March"); break; default: System.out.println("Invalid month"); }
1.3 반복문
- 코드 블록을 여러 번 실행하는 구조.
* for 문
for (int i = 1; i <= 5; i++) { System.out.println("Count: " + i); }
* while 문
int count = 1; while (count <= 5) { System.out.println("Count: " + count); count++; }
2. 간단한 연산자
- 자바에서 제공하는 기본 연산자들이다.
연산자 기능 예시 + 덧셈 a+b - 뺄셈 a-b * 곱셈 a*b / 나눗셈 (몫) a/b % 나머지 a%b == 같음 비교 a==b != 같지 않음 a!=b && 논리 AND a&&b ` ` (예제: 덧셈 계산기)
int num1 = 10; int num2 = 20; int sum = num1 + num2; System.out.println("Sum: " + sum); // 출력: Sum: 30
3. 간단한 실습 문제
* 문제 1: 두 수의 합 계산
- 두 정수를 입력받아 합을 출력하는 프로그램을 작성하세요.
(입력 예시)
10 20
(출력 예시)
30
(코드)
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter first number: "); int num1 = sc.nextInt(); System.out.print("Enter second number: "); int num2 = sc.nextInt(); int sum = num1 + num2; System.out.println("Sum: " + sum); } }
4. 백준 문제 도전
* 문제: 두 수 비교하기 (백준 1330)
* 문제 설명
- 두 정수 A와 B가 주어졌을 때, 다음 세 가지 결과 중 하나를 출력하세요.
- A > B → ">"
- A < B → "<"
- A == B → "=="
(입력 예시)
10 20
(출력 예시)
<
(코드)
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int A = sc.nextInt(); int B = sc.nextInt(); if (A > B) { System.out.println(">"); } else if (A < B) { System.out.println("<"); } else { System.out.println("=="); } } }
5. 추가 실습 문제
* 문제 1: 홀수 / 짝수 판별
* 문제 설명
- 사용자로부터 정수를 입력받아, 해당 정수가 홀수인지 짝수인지 출력하는 프로그램을 작성하세요.
(입력 예시)
10
(출력 예시)
짝수입니다.
(코드)
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("숫자를 입력하세요: "); int num = sc.nextInt(); if (num % 2 == 0) { System.out.println("짝수입니다."); } else { System.out.println("홀수입니다."); } } }
* 문제 2: 구구단 출력
* 문제 설명
- 사용자로부터 입력받은 숫자에 대한 구구단을 출력하는 프로그램을 작성하세요.
(입력 예시)
5
(출력 예시)
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 ... 5 * 9 = 45
(코드)
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("출력할 구구단 단수를 입력하세요: "); int n = sc.nextInt(); for (int i = 1; i <= 9; i++) { System.out.println(n + " * " + i + " = " + (n * i)); } } }
* 문제 3: 점수에 따른 학점 계산
* 문제 설명
- 사용자가 입력한 점수에 따라 학점을 계산하는 프로그램을 작성하세요.
- 조건
- 90점 이상: A
- 80점 이상 90점 미만: B
- 70점 이상 80점 미만: C
- 60점 이상 70점 미만: D
- 60점 미만: F
(입력 예시)
85
(출력 예시)
B
(코드)
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("점수를 입력하세요: "); int score = sc.nextInt(); 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 { System.out.println("F"); } } }
* 문제 4: 최소/최대값 찾기
* 문제 설명
- 사용자로부터 입력받은 3개의 숫자 중 최소값과 최대값을 출력하는 프로그램을 작성하세요.
(입력 예시)
12 7 15
(출력 예시)
최소값: 7 최대값: 15
(코드)
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("세 개의 숫자를 입력하세요: "); int num1 = sc.nextInt(); int num2 = sc.nextInt(); int num3 = sc.nextInt(); int min = Math.min(num1, Math.min(num2, num3)); int max = Math.max(num1, Math.max(num2, num3)); System.out.println("최소값: " + min); System.out.println("최대값: " + max); } }
* 문제 5: 특정 범위의 합 구하기
* 문제 설명
- 사용자로부터 두 개의 숫자를 입력받아, 해당 범위에 있는 모든 정수의 합을 구하세요.
(작은 수부터 큰 수까지의 합)
(입력 예시)
1 5
(출력 예시)
범위의 합: 15
(코드)
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("두 숫자를 입력하세요: "); int num1 = sc.nextInt(); int num2 = sc.nextInt(); int start = Math.min(num1, num2); int end = Math.max(num1, num2); int sum = 0; for (int i = start; i <= end; i++) { sum += i; } System.out.println("범위의 합: " + sum); } }
* 문제 6: 삼각형 출력
* 문제 설명
- 사용자로부터 숫자를 입력받아, 해당 숫자만큼의 높이를 가진 왼쪽 정렬된 삼각형을 출력하세요.
(입력 예시)
5
(출력 예시)
* ** *** **** *****
(코드)
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("삼각형의 높이를 입력하세요: "); int height = sc.nextInt(); for (int i = 1; i <= height; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); } } }
* 문제 7: 팩토리얼 계산
* 문제 설명
- 사용자로부터 숫자를 입력받아 해당 숫자의 팩토리얼을 계산하는 프로그램을 작성하세요.
(팩토리얼: n! = n x (n-1) x ... x 1)
(입력 예시)
5
(출력 예시)
5! = 120
(코드)
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("팩토리얼을 계산할 숫자를 입력하세요: "); int num = sc.nextInt(); int factorial = 1; for (int i = 1; i <= num; i++) { factorial *= i; } System.out.println(num + "! = " + factorial); } }
* 문제 8: 피보나치 수열
* 문제 설명
- 사용자로부터 입력받은 숫자까지의 피보나치 수열을 출력하는 프로그램을 작성하세요.
(피보나치 수열: 0, 1, 1, 2, 3, 5, 8, ...)
(입력 예시)
7
(출력 예시)
0 1 1 2 3 5 8
(코드)
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("출력할 피보나치 수열 항의 개수를 입력하세요: "); int n = sc.nextInt(); int a = 0, b = 1; System.out.print(a + " " + b + " "); for (int i = 3; i <= n; i++) { int next = a + b; System.out.print(next + " "); a = b; b = next; } } }
'코딩 테스트' 카테고리의 다른 글
Day 3 문자열 처리와 2차원 배열 강의 (0) 2024.12.22 Day 2 함수와 배열 (2) 2024.12.20