Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
hyebin-dev
java-practice
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
java-practice
/
week02
/
advanced
/
Computer.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
46 lines (38 loc) · 1.63 KB
main
Breadcrumbs
java-practice
/
week02
/
advanced
/
Computer.java
Copy path
Top
File metadata and controls
Code
Blame
46 lines (38 loc) · 1.63 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package week02.advanced;
public class Computer {
private int[] answer = new int [3];
public Computer(){
}
public int[] createAnswer() {
for (int i = 0; i < answer.length; i++) {
while (true) {
int num = (int)(Math.random()*9+1); // 1~9
boolean dup = false;
for (int j = 0; j < i; j++) {
if (answer[j] == num) {
dup = true; // while문 빠져나가기
break; // 바깥 for문으로
}
}
if (!dup) {
answer[i] = num; // 중복이 아니면 배열에 넣기
break;
}
}
}
return answer;
}
public int[] getAnswer() {
return answer;
}
// 23단계 (심화): 숫자 야구 게임의 완전한 책임 분리
// 문제: 1주차 14번 숫자 야구 게임을 세 개의 클래스로 분리하여 재설계하세요.
// 1. Computer.java: 정답 숫자를 생성하고(createAnswer()) 보관하는 역할(int[] answer).
// 2. Referee.java: Computer의 정답과 User의 추측을 비교하여 스트라이크/볼을 판정하는
// judge(int[] answer, int[] guess) static 메서드를 가진 역할.
// 3. BaseballGame.java: main 메서드를 포함하며, Computer 객체를 만들고
// while 루프를 돌며 사용자 입력을 받아 Referee를 통해 결과를 얻고 출력하는 게임 진행의 역할.
// 핵심 사고: 프로그램의 책임과 역할을 기준으로 클래스를 명확하게 분리하여(관심사의 분리),
// 코드의 재사용성과 유지보수성을 높이는 설계를 학습합니다.
// 힌트: 클래스 분리, 객체 간의 협력, static 메서드, while문, input, 배열
}
You can’t perform that action at this time.