Last active
March 3, 2023 15:58
-
-
Save zhyunk/891c7633f0ada42d46fdd01da05703dc to your computer and use it in GitHub Desktop.
과제 5. 달력 출력 프로그램
This file contains hidden or 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 java.util.Scanner; | |
import java.time.LocalDate; | |
/* | |
* 과제 5. 달력 출력 프로그램 | |
* | |
* 작성자 : 김지현 | |
* 작성일 : 2023-03-02 | |
* */ | |
public class MiniAssignment05 { | |
public static void main(String[] args) { | |
int year, month; | |
MyScanner my = new MyScanner(); | |
System.out.println("[달력 출력 프로그램]"); | |
year = my.getScannerReturnIntLength("달력의 년도를 입력해 주세요.(yyyy):", 4); | |
month = my.getScannerReturnIntRange("달력의 월을 입력해 주세요.(mm):", 1, 12); | |
my.close(); | |
System.out.println("\n"); | |
printAnyLocalDate(year, month); | |
} | |
public static void printAnyLocalDate(int year, int month) { | |
LocalDate anyDate = LocalDate.of(year, month, 1); | |
int startYoil = anyDate.getDayOfWeek().getValue(); | |
int lastDay = anyDate.withDayOfMonth(anyDate.lengthOfMonth()).getDayOfMonth(); | |
// 1:월요일, .. , 7: 일요일 -> 0: 일요일, 1:월요일, .. , 6: 토요일 | |
if (startYoil == 7) startYoil = 0; | |
// 출력 | |
System.out.println(String.format("[%d년 %02d월]", year, month)); | |
System.out.println(String.format("%s\t%s\t%s\t%s\t%s\t%s\t%s", "일", "월", "화", "수", "목", "금", "토")); | |
for (int i = 0; i < lastDay; ) { | |
for (int j = 0; j < 7; j++) { | |
if (i == 0) { | |
if (j < startYoil) { | |
System.out.print("\t"); | |
} else { | |
System.out.print(String.format("%02d\t", ++i)); | |
} | |
} else { | |
if (i == lastDay) break; | |
System.out.print(String.format("%02d\t", ++i)); | |
} | |
} | |
System.out.println(); | |
} | |
} | |
static class MyScanner { | |
String str; | |
Scanner scan; | |
MyScanner() { | |
scan = new Scanner(System.in); | |
} | |
public int getScannerReturnIntRange(String text, int min, int max) { | |
// min = 최소(포함) 범위 , max = 최대(포함) 범위 | |
str = ""; | |
while (true) { | |
System.out.print(text); | |
str = scan.nextLine(); | |
if (!str.replaceAll(" ", "").isEmpty() && str.replaceAll("[0-9]", "").isEmpty()) { | |
if (Integer.parseInt(str) < max + 1 && Integer.parseInt(str) > min - 1) | |
break; | |
} | |
} | |
return Integer.parseInt(str); | |
} | |
public int getScannerReturnIntLength(String text, int numLength) { | |
// numLength = 숫자 길이 | |
str = ""; | |
while (true) { | |
System.out.print(text); | |
str = scan.nextLine(); | |
if (!str.replaceAll(" ", "").isEmpty() && str.replaceAll("[0-9]", "").isEmpty() && str.length() == numLength) { | |
break; | |
} | |
} | |
return Integer.parseInt(str); | |
} | |
public void close() { | |
scan.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment