2019년 9월 1일 일요일

[JAVA] 배열 예제 - 월, 일을 입력 받고 1년이 며칠 남았는지 출력 / 100일 후 날짜 출력


import java.util.Scanner;

public class Day06Ex04 {
static Scanner scan = new Scanner(System.in);

public static void test01(String[] args) {

int[] days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int month, day;
int total = 0;

// ex)
// 월 입력 : 8
// 일 입력: 27
// -> 1년이 ?일 남았습니다.
System.out.println("월 입력 : ");
month = scan.nextInt();

System.out.println("일 입력 : ");
day = scan.nextInt();

for (int i = 0; i < month - 1; i++) {
total += days[i];
}
total += day;

System.out.println("1년이 " + (365 - total) + "일 남았습니다");


} // end of test01

public static void main(String[] args) {
// 과제 : 월, 일을 입력 받고 100일 후 날짜 출력
int[] days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int month, day;
int day2 = 100;

System.out.println("월 입력 : ");
month = scan.nextInt();

System.out.println("일 입력 : ");
day = scan.nextInt();

day2 = day2 -(days[month-1] -day); // 입력받은 첫 번째 달 따로 처리

while (day2 > days[month%12]) {
if ((month%12) == 0) {
month = 1;
} else {
month++;
} // if-else : 다음 년도로 넘어갔을 경우 처리

day = days[month - 1];
day2 = day2 - day;
} // while

month++;

System.out.println("입력하신 날짜의 100일 뒤는 " + month + "월 " + day2 + "일 입니다.");

} // end of test02

}

========================================================
<100일 뒤 날짜 출력 다른 코드>

import java.util.Scanner;

import static java.lang.System.out;

public class Day07Homework2 {
static Scanner scan = new Scanner(System.in);
public static void hw1(String[] args) {
// 100일 뒤 날짜 계산
Scanner scan = new Scanner(System.in);
int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int month, day; // 입력받을 날짜
int dMonth, dDay; // 100일 후 날짜
int total = 0;

out.print("Input month>>>");
month = scan.nextInt();

out.print("Input day>>>");
day = scan.nextInt();


out.printf("%d월%d일의 100일 후는", month, day);

// total에 현재 달의 남은 날짜를 저장.
// 현재 달 : month, 현재 달의 인덱스 :month-1
total = days[month-1] - day;
int i = month; // 현재 달의 다음 달.
while(total < 100){
i %= 12; // 12월 일 때 처리
total += days[i];
i++;
}
// 무조건 100보다 크다.
dDay = days[i-1]-(total-100);
dMonth = i;

scan.close(); // scanner 종료
out.printf("%d월%d일입니다.\n", dMonth, dDay);

} // end of hw1 - 날짜 세기 total에 더하는 법

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int month, day; // 입력받을 날짜
int dMonth, dDay; // 100일 후 날짜
int total = 100;

out.print("Input month>>>");
month = scan.nextInt();

out.print("Input day>>>");
day = scan.nextInt();

out.printf("%d월%d일의 100일 후는", month, day);

total = 100 - (days[month-1] - day); // 현재 달의 남은 날짜를 빼준다.
int i = month%12;

while(total > days[i]){
total = total - days[i];
i++;
i %=12;
}

dMonth = i + 1;
dDay = total;


out.printf("%d월%d일입니다.\n", dMonth, dDay);

scan.close(); // scanner 종료
} // end of hw2 - 날짜 세기 total에서 빼는 법

}

댓글 없음:

댓글 쓰기

[프로그래머스] 프린터 (자바/Java)

문제 설명 일반적인 프린터는 인쇄 요청이 들어온 순서대로 인쇄합니다. 그렇기 때문에 중요한 문서가 나중에 인쇄될 수 있습니다. 이런 문제를 보완하기 위해 중요도가 높은 문서를 먼저 인쇄하는 프린터를 개발했습니다. 이 새롭게 개발한 프린터는 아래와 같은...