Skip to content

Commit ff57f67

Browse files
committed
docs(coding-test): 코드업(CodeUp) Java 풀이 1071 ~ 1077 초안
1 parent 26aa25e commit ff57f67

File tree

1 file changed

+173
-0
lines changed
  • content/posts/java-coding-test-loop

1 file changed

+173
-0
lines changed
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
---
2+
title: '코드업(CodeUp) Java 풀이 1071 ~ 1077'
3+
date: '2026-03-03T23:00:41+09:00'
4+
categories: ["코딩 테스트"]
5+
tags: ["CodeUp", "Java", "반복문"]
6+
draft: true
7+
---
8+
9+
## 1071. 0 입력될 때 까지 무한 출력하기1
10+
11+
```java
12+
public class Main {
13+
public static void main(String[] args) {
14+
Scanner scanner = new Scanner(System.in);
15+
16+
while (true) {
17+
int n = scanner.nextInt();
18+
19+
if (n == 0) {
20+
break;
21+
}
22+
System.out.println(n);
23+
}
24+
25+
scanner.close();
26+
}
27+
}
28+
```
29+
30+
- `while`, `for`, `do~while` 3가지 선택지 중 `while`문을 사용했다.
31+
- 반복 횟수가 명확하면 `for`문으로 해결하는 방법이 편리하지만 명확하지 않기 때문에 `while`이 적합하다.
32+
33+
### 1. Sentinal Values를 사용하여 개선
34+
35+
```java
36+
public class Main {
37+
public static void main(String[] args) {
38+
Scanner scanner = new Scanner(System.in);
39+
40+
int n = scanner.nextInt();
41+
while (n != 0) {
42+
System.out.println(n);
43+
n = scanner.nextInt();
44+
}
45+
46+
scanner.close();
47+
}
48+
}
49+
```
50+
51+
- 무한루프 + `break` 조합이 아닌 `while` 헤더에 종료 조건을 작성하여 코드를 개선할 수 있다.
52+
- 종료 조건이 명확하고 불필요한 `break`문을 사용하지 않아도 흐름을 바로 알 수 있다.
53+
- 스트림/파일 읽기, 네트워크/서버 루프, Iterator기반 반복 등 자주 사용하는 방식이다.
54+
55+
## 1072. 정수 입력받아 계속 출력하기
56+
57+
```java
58+
public class Main {
59+
public static void main(String[] args) {
60+
Scanner scanner = new Scanner(System.in);
61+
62+
int count = scanner.nextInt();
63+
for (int i = 0; i < count; i++) {
64+
int number = scanner.nextInt();
65+
System.out.println(number);
66+
}
67+
68+
scanner.close();
69+
}
70+
}
71+
```
72+
73+
- Scanner는 어떻게 동작할까? (feat. Buffer)
74+
75+
## 1073. 0 입력될 때까지 무한 출력하기2
76+
77+
```java
78+
import java.util.Scanner;
79+
80+
public class Main {
81+
public static void main(String[] args) {
82+
Scanner scanner = new Scanner(System.in);
83+
84+
int number = scanner.nextInt();
85+
while (number != 0) {
86+
System.out.println(number);
87+
number = scanner.nextInt();
88+
}
89+
90+
scanner.close();
91+
}
92+
}
93+
```
94+
95+
## 1074. 정수 1개 입력받아 카운트 다운 출력하기1
96+
97+
```java
98+
public class Main {
99+
100+
public static void main(String[] args) {
101+
Scanner scanner = new Scanner(System.in);
102+
103+
int startNumber = scanner.nextInt();
104+
for (int current = startNumber; current >= 1; current--) {
105+
System.out.println(current);
106+
}
107+
108+
scanner.close();
109+
}
110+
}
111+
```
112+
113+
## 1075. 정수 1개 입력받아 카운트다운 출력하기2
114+
115+
```java
116+
import java.util.Scanner;
117+
118+
public class Main {
119+
public static void main(String[] args) {
120+
Scanner scanner = new Scanner(System.in);
121+
122+
int current = scanner.nextInt();
123+
while (current > 0) {
124+
current--;
125+
System.out.println(current);
126+
}
127+
128+
scanner.close();
129+
}
130+
}
131+
```
132+
133+
## 1076. 문자 1개 입력받아 알파벳 출력하기
134+
135+
```java
136+
public class Main {
137+
public static void main(String[] args) {
138+
Scanner scanner = new Scanner(System.in);
139+
140+
char endCharacter = scanner.next().charAt(0);
141+
for (char current = 'a'; current <= endCharacter; current++) {
142+
System.out.print(current + " ");
143+
}
144+
145+
scanner.close();
146+
}
147+
}
148+
```
149+
150+
- `for`문에 `char` 사용이 가능한 건 처음 알았다.
151+
152+
## 1077.
153+
154+
```java
155+
public class Main {
156+
public static void main(String[] args) {
157+
Scanner scanner = new Scanner(System.in);
158+
159+
int endNumber = scanner.nextInt();
160+
for (int i = 0; i <= endNumber; i++) {
161+
System.out.println(i);
162+
}
163+
164+
scanner.close();
165+
}
166+
}
167+
```
168+
169+
## 마치며
170+
171+
### 참고 자료
172+
173+
- [Understanding Sentinel Values in Java: A Practical Guide](https://www.oreateai.com/blog/understanding-sentinel-values-in-java-a-practical-guide/e5082a625e68c93bc2c9eb0fbe6fb2d8)

0 commit comments

Comments
 (0)