Page last modified: Dec 12 2020
목표
자바가 제공하는 제어문 학습하기
Table of contents
if/else statement
expression의 결과값이 true
이면 if문을 실행, false
이면 else문을 실행 (else문이 없으면 skip)
Syntax
if (expression) {
statement
}
if (expression) {
statement
} else {
statement
}
// e.g.
int age = 20;
if (age < 19) {
System.out.println("You are not an adult.");
} else {
System.out.println("You are not adult.");
}
else if
조건이 추가로 필요한 경우 사용
// e.g.
if (word.equals("a")) {
System.out.println("It's an apple");
} else if (word.equals("b")) {
System.out.println("It's a banana");
} else if (word.equals("c")) {
System.out.println("It's a carrot");
} else {
System.out.println("There are no corresponding results");
}
nested if/else statement
if와 else의 짝을 block{}
으로 명시하지 않는다면, else는 가까이에 있는 if와 짝이 된다
if(num > 0)
if(num == 1)
System.out.println("just one");
else
System.out.println("more than one"); // if(num == 1)와 짝, indentation 상관 X
switch statement
- if/else문과 비슷하나, 하나의 변수에 의해 처리가 나누어진다
- 해당하는 case가 없으면 default 실행
- case 끝에 break문이 없으면 인터프리터는 switch문이 끝났다고 인식하지 않기 때문에
이후에 매칭되는 case가 있는지 확인한다
switch문에서 비교가능한 타입
- byte, Byte
- short, Short
- int, Integer
- char, Character
- enum
- String
* null값이면 NullPointerException 발생
// e.g.
String word = "p";
switch (word) {
case "a":
System.out.println("It's an apple");
break;
case b":
System.out.println("It's a banana");
break;
case "c":
System.out.println("It's a carrot");
break;
default:
System.out.println("There are no corresponding results");
}
참고 https://www.baeldung.com/java-switch
while statement
반복적 동작을 가능하게 한다
Syntax
while (expression) {
statement
}
- expression의 결과값이
true
이면 statement를 실행,false
이면 skip - statement 실행 후 expression 재판단
∴ 무한루프에 빠지지 않도록 주의해야 함!
// e.g.
int count = 0;
while (count < 10) {
System.out.println(count + 1);
count++;
}
do statement
Syntax
do {
statement
} while (expression);
- 최초 한번은 statement 무조건 실행
- 1회 실행 후 expression의 결과값이
true
이면 statement를 다시 실행,false
이면 skip while (expression);
에서 끝에;
이 붙는다
// e.g.
int count = 0;
do {
System.out.println(count + 1);
count++;
} while (count < 10);
for statement
반복에 사용되는 변수의 초기화, 조건, 갱신에 대해 정의할 수 있다
Syntax
for(initialize; expression; update) {
statement
}
- 최초 실행)
initialize
→test
결과가 true (false면 exit) →statement
→update
- 2회 이상)
test
결과가 true (false면 exit) →statement
→update
// e.g.
for(int i = 0; i < 10; i++) {
System.out.println(i + 1);
}
foreach statement
객체의 collection을 반복할 때 사용 (객체가 null이 안 되도록 조심!)
Syntax
for( declaration : expression ) {
statement
}
특징
- 인덱스 사용 불가 (즉, statement안에 들어온 요소가 몇 번째 요소인지 알 수 없다)
- collection의 끝에서부터 거꾸로 반복 불가
int[] primes = new int[] {2,3,5,7,11,13,17,19,23,29};
for(int n : primes)
System.out.println(n);
참고 Java in a Nutshell 7th edition
과제 1. live-study 대시 보드를 만드는 코드를 작성하세요.
- 깃헙 이슈 1번부터 18번까지 댓글을 순회하며 댓글을 남긴 사용자를 체크 할 것.
- 참여율을 계산하세요. 총 18회에 중에 몇 %를 참여했는지 소숫점 두자리가지 보여줄 것.
- Github 자바 라이브러리를 사용하면 편리합니다.
- 깃헙 API를 익명으로 호출하는데 제한이 있기 때문에 본인의 깃헙 프로젝트에 이슈를 만들고 테스트를 하시면 더 자주 테스트할 수 있습니다.
해결 과정
- IntelliJ에서 Maven Project 생성
- pom.xml 파일에 Github 자바 라이브러리 추가
- 라이브러리 도큐먼트 참고하여 코드작성
- 이슈 1번 ~ 18번까지 커멘트를 단 사용자 아이디와 그 수를 취득
- 1주차 ~ 18주차까지 참여정보를 담는 WeekInfo 클래스 생성
- 1주차 ~ 18주차까지 참여정보를 담는 WeekInfo 클래스 생성
- 마크다운 표 형식에 맞게 출력 ( = dashboard 출력)
- 이슈 1번 ~ 18번까지 커멘트를 단 사용자 아이디와 그 수를 취득
- 깃헙 저장소에 이슈 만들고 동작 테스트
결과물
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.kohsuke.github.GHIssue; | |
import org.kohsuke.github.GHIssueComment; | |
import org.kohsuke.github.GitHub; | |
import org.kohsuke.github.GitHubBuilder; | |
import java.io.IOException; | |
import java.util.*; | |
public class Dashboard { | |
private static final String PERSONAL_TOKEN = "WRITE_YOUR_PERSONAL_TOKEN_HERE"; | |
private static final String REPOSITORY = "whiteship/live-study"; | |
public static final int[] ISSUE_ID = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}; | |
private static HashMap<String, WeekInfo> weekInfoPerUser = new HashMap<String, WeekInfo>(); | |
private static int[] weeklySubmittedUserCount = new int[ISSUE_ID.length]; | |
public static void main(String[] args) throws IOException { | |
getDashboardDataFromGithub(); | |
printDashboard(); | |
} | |
public static void getDashboardDataFromGithub() throws IOException { | |
// 깃허브 연결 | |
GitHub github = new GitHubBuilder().withOAuthToken(PERSONAL_TOKEN).build(); | |
// 이슈에 커멘트를 단 유저의 아이디와 그 수를 취득 | |
for (int i = 0; i < ISSUE_ID.length; i++) { | |
GHIssue issue = github.getRepository(REPOSITORY).getIssue(ISSUE_ID[i]); | |
List<GHIssueComment> allComments = issue.getComments(); | |
if (allComments == null) continue; | |
HashSet<String> submittedUser = new HashSet<String>(); | |
for (GHIssueComment comment : allComments) { | |
String userID = comment.getUser().getLogin(); | |
submittedUser.add(userID); | |
if (weekInfoPerUser.containsKey(userID)) weekInfoPerUser.get(userID).submit(i); | |
else weekInfoPerUser.put(userID, new WeekInfo(i)); | |
} | |
weeklySubmittedUserCount[i] = submittedUser.size(); | |
} | |
} | |
public static void printDashboard() { | |
String title = "| 참여자 |"; | |
String dash = "| --- |"; | |
for (int i = 0; i < ISSUE_ID.length; i++) { | |
title += " " + (i + 1) + "주차<br>" + weeklySubmittedUserCount[i] + "/" + weekInfoPerUser.size() + "<br>"; | |
String submitRatio = String.format("%.2f", (float) weeklySubmittedUserCount[i] / weekInfoPerUser.size() * 100) + "%"; | |
title += "(" + submitRatio + ") |"; | |
dash += " :---: |"; | |
} | |
title += " 참석율 |"; | |
dash += " --- |"; | |
System.out.println(title); | |
System.out.println(dash); | |
SortedSet<String> sortedUserID = new TreeSet<String>(weekInfoPerUser.keySet()); | |
for (String userID : sortedUserID) { | |
String row = "| " + userID + " |"; | |
for (boolean submission : weekInfoPerUser.get(userID).getWeek()) { | |
if (submission) row += ":white_check_mark:"; | |
row += "|"; | |
} | |
row += " " + weekInfoPerUser.get(userID).getSubmitRatio() + " |"; | |
System.out.println(row); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WeekInfo { | |
private int TOTAL_WEEK = Dashboard.ISSUE_ID.length; | |
private boolean[] week = new boolean[TOTAL_WEEK]; | |
private int submitCount = 0; | |
WeekInfo(int weekIdx) { | |
submit(weekIdx); | |
} | |
public boolean[] getWeek() { | |
return week; | |
} | |
public void submit(int weekIdx) { | |
if (!week[weekIdx]) { | |
week[weekIdx] = true; | |
submitCount++; | |
} | |
} | |
public String getSubmitRatio() { | |
return String.format("%.2f", (float) submitCount / TOTAL_WEEK * 100) + "%"; | |
} | |
} |
참여자 | 1주차 193/224 (86.16%) | 2주차 155/224 (69.20%) | 3주차 148/224 (66.07%) | 4주차 104/224 (46.43%) | 5주차 0/224 (0.00%) | 6주차 0/224 (0.00%) | 7주차 0/224 (0.00%) | 8주차 0/224 (0.00%) | 9주차 0/224 (0.00%) | 10주차 0/224 (0.00%) | 11주차 0/224 (0.00%) | 12주차 0/224 (0.00%) | 13주차 0/224 (0.00%) | 14주차 0/224 (0.00%) | 15주차 0/224 (0.00%) | 16주차 0/224 (0.00%) | 17주차 0/224 (0.00%) | 18주차 0/224 (0.00%) | 참석율 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0417taehyun | ✅ | ✅ | ✅ | 16.67% | |||||||||||||||
1031nice | ✅ | ✅ | ✅ | ✅ | 22.22% | ||||||||||||||
2yeseul | ✅ | ✅ | 11.11% | ||||||||||||||||
372dev | ✅ | ✅ | ✅ | 16.67% | |||||||||||||||
9m1i9n1 | ✅ | ✅ | ✅ | 16.67% | |||||||||||||||
Ahnyezi | ✅ | 5.56% |
~ 이후 생략 ~