Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 스프링부트 오류
- 스프링 jsp 연결
- spring jsp
- 자바정렬
- html video thumbnail
- video tag
- 자바 #Java #Scanner #스캐너 #자바알파벳입력
- Failed to configure a DataSource
- selection does not contain a main type
- 자바 순위정렬
- 스프링부트 jsp 연결
- html input
- SpringBoot Mybatis
- src/main/java
- @PathVariable
- 정수 자료형
- 비디오 태그 썸네일
- sql 테이블명 바꾸기
- 자바마스터
- 스프링 jsp
- 순위정렬
- jsp 연결
- MySQL 테이블명 바꾸기
- 자바
- 비디오 태그 이미지
- 정신차리고공부하자 #개발자되기 #몰입하자
- 스프링부트
- HTML
- 랭킹정렬
- 스프링부트 마이바티스
Archives
- Today
- Total
쌤리
코드업 3019 풀이 본문
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
ScheduleItem[] scheduleItems = getScheduleItemsFromInput();
sort(scheduleItems);
print(scheduleItems);
}
}
- 함수를 활용해 메인에서 실행하려는 바가 뭔지 명확하게 나타내기 위해서 위와 같이 3가지 함수(메서드)로 나뉜다.
- 1) 입력받은 값을 스케줄 목록 배열에 넣는다. 2) 입력받은 스케줄을 정렬한다(sort) 3) 출력한다
private static void print(ScheduleItem[] scheduleItems) {
for (int i = 0; i < scheduleItems.length; i++) {
System.out.println(scheduleItems[i].name);
}
}
private static void sort(ScheduleItem[] scheduleItems) {
for (int i = scheduleItems.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (scheduleItems[j].isEarlierThanOnDic(scheduleItems[j + 1])) {
ScheduleItem temp = scheduleItems[j];
scheduleItems[j] = scheduleItems[j + 1];
scheduleItems[j + 1] = temp;
}
}
}
}
private static ScheduleItem[] getScheduleItemsFromInput() {
Scanner scanner = new Scanner(System.in);
int scheduleItemsLen = scanner.nextInt();
scanner.nextLine();
ScheduleItem[] scheduleItems = new ScheduleItem[scheduleItemsLen];
for (int i = 0; i < scheduleItemsLen; i++) {
ScheduleItem scheduleItem = new ScheduleItem();
scheduleItem.name = scanner.next();
scheduleItem.year = scanner.next();
scheduleItem.month = scanner.next();
scheduleItem.day = scanner.next();
scheduleItem.year = String.format("%04d", Integer.parseInt(scheduleItem.year));
scheduleItems[i] = scheduleItem;
}
scanner.close();
return scheduleItems;
}
}
class ScheduleItem {
String name;
String year;
String month;
String day;
public String getCompareStr() {
return year + month + day + name;
}
public boolean isEarlierThanOnDic(ScheduleItem other) {
String str = this.getCompareStr();
String str2 = other.getCompareStr();
return str.compareTo(str2) > 0;
}
}
- 이클립스를 활용해 자동으로 함수를 생성.
- 각 함수의 기능에 맞게 필요한 코드를 작성한다.
'자바 JAVA > 자바 기초' 카테고리의 다른 글
추상 클래스, abstract class (0) | 2020.04.24 |
---|---|
클래스와 객체 Class & Object (0) | 2020.04.24 |
객체, 클래스 (0) | 2020.04.16 |
Scanner(스캐너)를 이용한 char 입력: next().charAt(0); (0) | 2020.04.14 |
printf (1) | 2020.04.13 |
Comments