Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
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.