TIL/JAVA
23.04.25
J1-H00N
2023. 4. 25. 20:51
캘린더 만들기
기본 입력
public class Calendar {
public static void main(String[] args) {
System.out.println("일 월 화 수 목 금 토");
System.out.println("------------------");
System.out.println(" 1 2 3 4 5 6 7");
System.out.println(" 8 9 10 11 12 13 14");
System.out.println("15 16 17 18 19 20 21");
System.out.println("22 23 24 25 26 27 28");
}
}
숫자 입력하고 더하기
public class Add {
public static void main(String[] args) {
int a,b;
Scanner scanner = new Scanner(System.in);
String s1, s2;
System.out.println("두 수를 입력해 주세요");
s1 = scanner.next();
s2 = scanner.next();
System.out.println(s1 + ", " + s2);
a = Integer.parseInt(s1);
b = Integer.parseInt(s2);
// int c = a+b;
// System.out.println(s1 + " + " + s2 + " = " + c);
// System.out.printf("두 수의 합은 %d입니다.", a+b);
System.out.printf("%d와 %d의 합은 %d입니다.", a, b, a+b);
scanner.close(); // scanner 작업을 끝낸후 필요
}
}
한 달의 일 수 구하기
package Javajigi.Calendar;
import java.util.Scanner;
public class DaysOfMonth {
public static final int[] MAX_DAYS = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public int getMaxDaysOfMonth(int month) {
return MAX_DAYS[month - 1];
}
public static void main(String[] args) {
// int b = 30;
// int c = 31;
// int d = 28;
// if (month<1) {
// System.out.println("1 이상 12 이하의 수를 입력해주세요");
// } else if (month>12) {
// System.out.println("1 이상 12 이하의 수를 입력해주세요");
// } else if (month < 8) {
// if (month == 2) {
// System.out.printf("%d달은 %d일까지 있습니다.", month, d);
// } else if (month%2 == 0) {
// System.out.printf("%d달은 %d일까지 있습니다.", month, b);
// } else {
// System.out.printf("%d달은 %d일까지 있습니다.", month, c);
// }
// } else {
// if (month % 2 == 0) {
// System.out.printf("%d달은 %d일까지 있습니다.", month, c);
// } else {
// System.out.printf("%d달은 %d일까지 있습니다.", month, b);
// }
// }
String prompt = "> ";
Scanner scanner = new Scanner(System.in);
DaysOfMonth cal = new DaysOfMonth();
while (true) {
System.out.println("달을 입력해 주세요");
System.out.print(prompt);
int month = scanner.nextInt();
if (month == -1) {
break;
}
if (month > 12) {
continue;
}
System.out.printf("%d월은 %d일까지 있습니다.", month, cal.getMaxDaysOfMonth(month));
}
System.out.println("반복이 종료되었습니다.");
scanner.close();
}
}
기본달력틀 만들기
package Javajigi.Calendar;
import java.util.Scanner;
public class Promptex {
private final static String PROMPT="cal> ";
public void rumPrompt() {
Scanner scanner = new Scanner(System.in);
Calendarex cal = new Calendarex();
while (true) {
System.out.println("년도를 입력해 주세요");
System.out.print(PROMPT);
int year = scanner.nextInt();
System.out.println("달을 입력해 주세요");
System.out.print(PROMPT);
int month = scanner.nextInt();
if (month == -1) {
break;
}
if (month > 12) {
continue;
}
cal.printCalendar(year, month);
}
System.out.println("반복이 종료되었습니다.");
scanner.close();
}
public static void main(String[] args) {
Promptex p = new Promptex();
p.rumPrompt();
}
}
package Javajigi.Calendar;
public class Calendarex {
private static final int[] MAX_DAYS = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
private static final int[] LEAP_MAX_DAYS = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public boolean isLeepYear(int year) {
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
public int getMaxDaysOfMonth(int year, int month) {
if (isLeepYear(year)) {
return LEAP_MAX_DAYS[month - 1];
} else return MAX_DAYS[month - 1];
}
public void printCalendar(int year, int month) {
System.out.printf(" <<%4d년%3d월>>\n", year, month);
System.out.println(" SU MO TU WE TH FR SA");
System.out.println("-----------------------");
int maxday = getMaxDaysOfMonth(year, month);
for(int i = 1; i <= maxday; i++) {
System.out.printf("%3d", i);
if (i%7 == 0) {
System.out.println();
}
}
System.out.println();
// System.out.println(" 1 2 3 4 5 6 7");
// System.out.println(" 8 9 10 11 12 13 14");
// System.out.println("15 16 17 18 19 20 21");
// System.out.println("22 23 24 25 26 27 28");
}
}