Language/JAVA
[JAVA]날짜입력프로그램/요일구하기/배열예제
영보로그
2020. 7. 1. 17:16
반응형
* 문제
요일 구하기 요약 2020 1) 전년도까지의 총 날수 - 1년도 1월 1일~~2019년도 12월 31일까지 계산 2) 전달까지의 총 날수 - 2020년 1월 1일 ~~ 2020년 6월 30일까지 3) 입력된 day까지 총 날수 구한다 예) 1년도 1월 1일 ==> 월요일로 시작 |
* 코드
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
import java.util.*;
public class 자바배열 {
public static void main(String[] args) {
//년도, 월, 일을 받는 변수 필요
int year=0,month=0,day=0;
//사용자로부터 요청값을 받는다
Scanner scan=new Scanner(System.in);
System.out.println("년도 입력:");
year=scan.nextInt();
//scan.nextInt() ==> 키보드로 입력된 정수값을 읽어오는 역할
System.out.println("월 입력 :");
month=scan.nextInt();
System.out.println("일 입력 :");
day=scan.nextInt();
System.out.println(year+"년도 "+month+"월 "+day+"일");
//전년도 까지의 총 합
int total=(year-1)*365
+(year-1)/4
-(year-1)/100
+(year-1)/400;
int[] lastDay= {31,28,31,30,31,30,31,31,30,31,30,31};
if((year%4==0 && year%100!=0)||(year%400==0)) //윤년조건
{
lastDay[1]=29;
}
else
{
lastDay[1]=28;
}
for(int i=0;i<month-1;i++)
{
total+=lastDay[i];
}
//입력된 DAY
total+=day;
//요일구하기
int week=total%7;
char[] strWeek= {'일','월','화','수','목','금','토'};
System.out.println(strWeek[week]+"요일입니다");
}
}
|
cs |
* 실행 화면
반응형