백지부터 시작하는 이세계 코딩 생활
날짜 계산기 본문
package s0429;
import java.util.Scanner;
public class Ch05_HW {
static int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
static int year = 0;
static int month = 0;
static int day = 0;
static int integDay = 0;
static int afterDay = 0;
static int tempDay = 0;
static int diffDay = 0;
static int dayTotal = 0;
static int newDiffDay = 0;
static int newMonth = 0;
static int newDay = 0;
static int newTempDay = 0;
static int index = 0;
static int newAfterDay = 0;
static int inputDay = 0;
static int eventNum = 0;
public static void Order(String[] args) {
// TODOAuto-generated method stub
// HW
// 월,일 입력 :4,29
// HW_1 : 몇일 후 날짜를 알고 싶니? : ex) 200 (일)
// HW_2 : 100일 이후의 날짜는?
// HW_3 : 200일 이후의 날짜는 ?월 ?일 입니다.
// defualt_HW
// 2차원 배열과 함수 (예습)
// 배열 (복습)
}// end main
public static void main(String[] args) {
System.out.println("이벤트를 선택: 1 또는 2");
System.out.println("이벤트1 : 100일 후 날짜를 확인");
System.out.println("이벤트2 : 입력한일 이후 날짜를 확인");
Scanner sc = new Scanner(System.in);
eventNum = sc.nextInt();
switch (eventNum) {
case 1:
event1(args);
break;
case 2:
event2(args);
break;
default:
System.out.println("잘못된 입력, 종료합니다.");
break;
}
}// end main
public static void event1(String[] args) {
// TODOAuto-generated method stub
Scanner sc = new Scanner(System.in);
// 월, 일을 받고 프린트.
System.out.print("month:");
month = sc.nextInt();
System.out.print("day:");
day = sc.nextInt();
// *----------------------------이벤트 1------------------------------*
if (month > 13 || day > 32) {
System.out.println("잘못된 입력");
} else {
// 요일수로 환산.
for (index = 0; index < month; index++) {
dayTotal+= days[index];
}
System.out.println("print,dayTotal: " + dayTotal);
for (index = 0; index < (month - 1); index++) {
tempDay+= days[index];
}
integDay = tempDay + day;
afterDay = integDay + 100;
System.out.println("print,integDay: " + integDay);
System.out.println("print,after 100days: " + afterDay);
// 입력받은 날로부터 100일 후 날짜 프린트.
// 괄호 없으면 계산없이 프린트함. print = integ_day + day100 (ie. 1100)
// 괄호 있으면 계산해서 프린트함. print = integ_day + day+100 (ie. 101)
// 1year 고정값 계산 : 365입력 배열 연습겸 코드 사용.
for (index = 0; index < days.length; index++) {
year+= days[index];
}
System.out.println("printyear: " + year);
// 100일후는 ~월, ~일로 환산.
if (afterDay < year) {
diffDay= dayTotal - integDay;
System.out.printf("after100days : %d월%d일", (month + 3), diffDay);
System.out.println();
} else {
System.out.println("overdate,해를 넘깁니다.");
// afterDay가 365일을 넘어갔을 때.
newDay= afterDay - year;
System.out.println("print,newDay: " + newDay);
newCalendar(args);
}
}
// *--------------------------------------------------------------------*
}// end main
public static void newCalendar(String[] args) {
if (newDay > 29 && newDay > 31) {
}
// 1월
if (newDay > 0 && newDay < 31) {
newMonth = 1;
for (index = 0; index < (newMonth - 1); index++) {
newTempDay= days[index];
}
//System.out.println("print, newtempday: " + newTempDay);
newDiffDay = newDay - newTempDay;
System.out.printf("%d월%d일", newMonth,newDiffDay);
}
// 2월
if (newDay > 31 && newDay < 90) {
newMonth = 2;
for (index = 0; index < (newMonth - 1); index++) {
newTempDay= days[index];
}
//System.out.println("print, newtempday: " + newTempDay);
newDiffDay = newDay - newTempDay;
System.out.printf("%d월%d일", newMonth,newDiffDay);
}
// 3월
if (newDay > 59 && newDay < 120) {
newMonth = 3;
for (index = 0; index < (newMonth - 1); index++) {
newTempDay= days[index];
}
//System.out.println("print, newtempday: " + newTempDay);
newDiffDay = newDay - newTempDay;
System.out.printf("%d월%d일", newMonth,newDiffDay);
}
// 4월
if (newDay > 90 && newDay < 151) {
newMonth = 4;
for (index = 0; index < (newMonth - 1); index++) {
newTempDay= days[index];
}
//System.out.println("print, newtempday: " + newTempDay);
newDiffDay = newDay - newTempDay;
System.out.printf("%d월%d일", newMonth,newDiffDay);
}
// 5월
if (newDay > 120 && newDay < 181) {
newMonth = 5;
for (index = 0; index < (newMonth - 1); index++) {
newTempDay= days[index];
}
//System.out.println("print, newtempday: " + newTempDay);
newDiffDay = newDay - newTempDay;
System.out.printf("%d월%d일", newMonth,newDiffDay);
}
// 6월
if (newDay > 151 && newDay < 212) {
newMonth = 6;
for (index = 0; index < (newMonth - 1); index++) {
newTempDay= days[index];
}
//System.out.println("print, newtempday: " + newTempDay);
newDiffDay = newDay - newTempDay;
System.out.printf("%d월%d일", newMonth,newDiffDay);
}
// 7월
if (newDay > 181 && newDay < 243) {
newMonth = 7;
for (index = 0; index < (newMonth - 1); index++) {
newTempDay= days[index];
}
//System.out.println("print, newtempday: " + newTempDay);
newDiffDay = newDay - newTempDay;
System.out.printf("%d월%d일", newMonth,newDiffDay);
}
// 8월
if (newDay > 212 && newDay < 273) {
newMonth = 8;
for (index = 0; index < (newMonth - 1); index++) {
newTempDay= days[index];
}
//System.out.println("print, newtempday: " + newTempDay);
newDiffDay = newDay - newTempDay;
System.out.printf("%d월%d일", newMonth,newDiffDay);
}
// 9월
if (newDay > 243 && newDay < 304) {
newMonth = 9;
for (index = 0; index < (newMonth - 1); index++) {
newTempDay= days[index];
}
//System.out.println("print, newtempday: " + newTempDay);
newDiffDay = newDay - newTempDay;
System.out.printf("%d월%d일", newMonth,newDiffDay);
}
// 10월
if (newDay > 273 && newDay < 334) {
newMonth = 10;
for (index = 0; index < (newMonth - 1); index++) {
newTempDay= days[index];
}
//System.out.println("print, newtempday: " + newTempDay);
newDiffDay = newDay - newTempDay;
System.out.printf("%d월%d일", newMonth,newDiffDay);
}
// 11월
if (newDay > 304 && newDay < 365) {
newMonth = 11;
for (index = 0; index < (newMonth - 1); index++) {
newTempDay= days[index];
}
//System.out.println("print, newtempday: " + newTempDay);
newDiffDay = newDay - newTempDay;
System.out.printf("%d월%d일", newMonth,newDiffDay);
}
// 12월
if (newDay > 334 && newDay < 367) {
newMonth = 12;
for (index = 0; index < (newMonth - 1); index++) {
newTempDay= days[index];
}
//System.out.println("print, newtempday: " + newTempDay);
newDiffDay = newDay - newTempDay;
System.out.printf("%d월%d일", newMonth,newDiffDay);
}
}// end main
public static void event2(String[] args) {
Scanner sc = new Scanner(System.in);
// 월, 일을 받고 프린트.
System.out.print("month:");
month = sc.nextInt();
System.out.print("day:");
day = sc.nextInt();
// *----------------------------이벤트 2------------------------------*
if (month > 13 && day > 32) {
System.out.println("잘못된 입력");
} else {
// 요일수로 환산.
for (index = 0; index < month; index++) {
dayTotal+= days[index];
}
System.out.println("print,dayTotal: " + dayTotal);
for (index = 0; index < (month - 1); index++) {
tempDay+= days[index];
}
System.out.print("원하는 이후 일수를 입력: ");
inputDay = sc.nextInt();
integDay = tempDay + day;
afterDay = integDay + inputDay;
System.out.println("print,afterDay: " + afterDay);
// 입력받은 날로부터 100일 후 날짜 프린트.
// 괄호 없으면 계산없이 프린트함. print = integ_day + day100 (ie. 1100)
// 괄호 있으면 계산해서 프린트함. print = integ_day + day+100 (ie. 101)
// 1year 고정값 계산 : 365입력 배열 연습겸 코드 사용.
for (index = 0; index < days.length; index++) {
year+= days[index];
}
// 입력받은 일 이후는 ~월, ~일로 환산.
if (afterDay < year) {
diffDay= dayTotal - integDay;
System.out.printf("입력일 이후 : %d월%d일", month, diffDay);
System.out.println();
} else {
System.out.println("overdate,해를 넘깁니다.");
// afterDay가 365일을 넘어갔을 때.
newDay= afterDay - year;
System.out.println("print,newDay: " + newDay);
newCalendar(args);
}
}
// *--------------------------------------------------------------------*
}
}// end class
'JAVA > 조건문' 카테고리의 다른 글
성적 입출력 (0) | 2020.08.08 |
---|---|
조건문, 스무고개 만들기 (0) | 2020.08.08 |
Comments