본문 바로가기

카테고리 없음

날짜 계산하기

일 년. 총 합 365일 구하기 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package day_06;
public class test {
    
    public static void main(String[] args) {
        
        int[] days= {31,28,31,30,31,30,31,31,30,31,30,31};
        int total= 0;
            
            for(int i=0; i<days.length; i++) {
                total += days[i];
            }
            
            System.out.println("total: " + total);
       }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

입력한 달의 일수 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package day_06;
import java.util.Scanner;
public class test {
    
    public static void main(String[] args) {
        
        Scanner scanner= new Scanner(System.in);
        
        int[] days= {31,28,31,30,31,30,31,31,30,31,30,31};
        int total= 0;
        int month= 0; 
            
        // 월을 입력 받아서 해당 월의 날짜 수를 출력한다. 
        System.out.print("월 입력: ") ; 
        month= scanner.nextInt();
        
        
        //2를 입력받았다면> 2월은 28일까지 있습니다. 라고 출력하려면
        System.out.println(month+ "월은 " +days[month-1]+ "일까지 있습니다.");
    }
}
    
 
cs

 

 

 

 

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
package day_06;
import java.util.Scanner;
public class test {
    
    public static void main(String[] args) {
        
        Scanner scanner= new Scanner(System.in);
        
        int[] days= {31,28,31,30,31,30,31,31,30,31,30,31};
        int total= 0;
        int month= 0; 
        int day= 0;
            
        // 월 - 일 입력 >> 1월 1일부터 월 - 일까지는 총 total일 입니다. 
        System.out.println("월 입력: ");
        month= scanner.nextInt();
        System.out.print("일 입력: ");
        day= scanner.nextInt();
        
        total = day; // 입력받은 월의 날짜 입력.
        for(int i=0; i<month-1; i++) { 
            total += days[i];
            
        }
        System.out.println("1월 1일부터 "+month+"월" +day+ "일 까지는 총 "+total+"일입니다.");
        
    }
}
    
cs

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
package day_06;
import java.util.Scanner;
public class test {
    
    public static void main(String[] args) {
        
        Scanner scanner= new Scanner(System.in);
        
        int[] days= {31,28,31,30,31,30,31,31,30,31,30,31};
        int total= 0;
        int month= 0; 
        int day= 0;
        System.out.println("월 입력: ");
        month= scanner.nextInt();
        System.out.println("일 입력: ");
        day= scanner.nextInt();
        
        //현재달의 남은 날자
        total= days[month-1] - day;
        
        //현재달의 처리가 끝났고, 다음 달에 대한 것을 계산한다
        for(int i= month; i<days.length; i++ ) {
            total += days[i];
            
        }
        System.out.println("1년의 남은 날짜는 "+total+"일입니다");
        
    }    
}
cs

 

입력한 일로부터 100일 후 달 /일 보여주기

 

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
package day_06;
import java.util.Scanner;
public class test {
    
    public static void main(String[] args) {
        
        Scanner scanner= new Scanner(System.in);
        
        int[] days= {31,28,31,30,31,30,31,31,30,31,30,31};
        int total= 0;
        int month= 0; 
        int day= 0;
        System.out.println("월 입력: ");
        month= scanner.nextInt();
        System.out.println("일 입력: ");
        day= scanner.nextInt();
        // 월 - 일 입력 후 100일 후를 출력한다. 
        // 방법1) total에 100을 채워두고 빼나간다
                
                
                
        total= 100 - (days[month-1] -day);  // 현재달의 남은 날자를 100에서 빼서 total에 넣어줌  
        int i= month % 12; // 12가 되면 0이됨
        while(days[i] < total) { //days의 i번지에 있는 값이 total보다는 커야함. 그래야 뺄 수 있으니까
            total -= days[i++]; // 빼줬으면 증가시켜줘야함
            i = i% 12; // 그리고 비교해야하니까. 12가되면 다시 0이 되도록. 
        }
                
        int nextDay= total;
        int nextMonth= i+1; 
                
        System.out.println("100일 후는"+nextMonth+"월 "+nextDay+"일 입니다.");
                
    }
}
cs
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
package day_06;
import java.util.Scanner;
public class test {
    
    public static void main(String[] args) {
        
        Scanner scanner= new Scanner(System.in);
        
        int[] days= {31,28,31,30,31,30,31,31,30,31,30,31};
        int total= 0;
        int month= 0; 
        int day= 0;
        System.out.println("월 입력: ");
        month= scanner.nextInt();
        System.out.println("일 입력: ");
        day= scanner.nextInt();
        // 월 - 일 입력 후 100일 후를 출력하기
        // 100이 될떄가지 누적하기 , 100이 넘으면 계산한 달에서 넘치는 만큼 빼준다. 
                
        // 해당월의 남은 날짜를 누적한다.
        total= days[month-1] -day;
        int i= 0;
        for(i=month; total<100; i++) {
            i = i %12; // i가 12가되면 days 12번지가 없엉서 에러가나니까
            total += days[i];
        }
                
        int nextMonth= i;
        int nextDay= days[i-1] - (total - 100); //해당 월의 총 날짜수에서 100에서 넘치는만큼 빼줌
        System.out.println("100일 후는 "+nextMonth+"월 "+nextDay+"일 입니다.");
    }
            
                
    }
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs