주민등록번호 입력 

jumin="901230-1837264" 로 입력받음


Q1. 정확히 입력했는지 확인(자릿수 확인, -입력확인)

Q2. 생년월일 출력

Q3. 성별을 확인하고 출력

Q4. 미성년자 확인

Q5. 나이 출력

Q6. 정확히 입력했는지 확인(공백처리, 정확한 숫자입력확인)


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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.Scanner;
 
public class Ex02 {
 
    static String myNum;
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        for(int i=0;i<5;i++){
            System.out.print("주민등록번호 입력(000000-0000000): ");
            myNum = sc.nextLine().trim();    //앞뒤 공백 제거
            
            String err = "정확하지가 않습니다.다시 입력해주세요";
            if(myNum.indexOf("-")!=6 || myNum.length()!=14){        //6번째 자리에 '-'가 없거나 총길이가 14개가 아니면 오류
                System.out.println(err);
            }else if(Integer.parseInt(myNum.substring(2,4))>12 || Integer.parseInt(myNum.substring(4,6))>31){    //월 12이상,일 31이상 일경우 오류
                System.out.println(err);
            }else if(Integer.parseInt(myNum.substring(7,8))>6){        //성별을 나타내는 숫자가 6이상이면 오류
                System.out.println(err);
            }
            else{
                birth();    //생년월일
                mf();        //성별
                bebe();        //미성년자 & 나이
                break;
            }
            if(i==4){
                System.out.println("종료합니다. 정확히 확인 후 다시 이용해 주십시오.");
            }
        }
    }//main end
    
    public static void birth(){    //생년월일 출력
        int y = 0;
        System.out.print("생년월일: ");
        System.out.println(myNum.substring(0,2)+"년"+myNum.substring(2,4)+"월"+myNum.substring(4,6)+"일");
    }//birth end
    
    public static void mf(){    //성별
        char c = myNum.charAt(7);
        
        System.out.print("성별: ");
        if(c=='1'||c=='3'){
            System.out.println("남자");
        }else if(c=='2'||c=='4'){
            System.out.println("여자");
        }else{
            System.out.println("외국인");
        }
    }//mf end
    
    public static void bebe(){        //미성년자확인 & 나이 출력
        int age = Integer.parseInt(myNum.substring(0,2));
                
        if(age<=16){
            age = Integer.parseInt(20+myNum.substring(0,2));    //년도가 16년보다 작으면 2000년대 출생
        }else{
            age = Integer.parseInt(19+myNum.substring(0,2));    //년도가 16년보다 크면 1900년대 출생
        }
        
        age = 2016-age+1;
        if(age>=19){
            System.out.println("성년 입니다.");
        }else{
            System.out.println("미성년자 입니다.");
        }
        System.out.println("나이: "+age);
    }//bebe end
}//class end
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.Scanner;
 
public class Ex02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input=null;
        int cnt =0;
        boolean ck = false;
        while(cnt++<5){
            System.out.println("주민번호를 입력해주세요");
            System.out.println("\tex)901230-1837264");
            System.out.print(">>");
            input = sc.nextLine().trim();
            for(int i=0; i<input.length(); i++){
                if(i==6){continue;}
                switch (input.charAt(i)) {
                    case '0'case '1'case '2'case '3'case '4'
                    case '5'case '6'case '7'case '8'case '9':
                        if(i==0){ck=true;}else{ck = ck&&true;} break;
                    default : ck=false;
                } 
            }
            if(ck&&input.length()==14&&input.charAt(6)=='-'){break;}
            System.out.println(cnt+"회 입력이 잘못되셨습니다");
            if(cnt==5){return;}
            System.out.println("확인후 재입력 바랍니다\t");
        }
        System.out.println("당신의 생년월일:");
        String[] st = {"년","월","일"};
        String[] st2 = {"","",""};
        for(int i=0; i<6; i++){
            st2[i/2]+=input.charAt(i);
        }
        for(int i=0; i<st2.length; i++){
            st2[i]+=st[i];
        }
        for(int i=0; i<st2.length; i++){
            System.out.print(st2[i]);
        }
        System.out.println();
        System.out.print("당신의 성별은 ");
        if(input.charAt(7)=='1'){
            System.out.print("남자");
        }else if(input.charAt(7)=='2'){
            System.out.print("여자");
        }else{
            System.out.print("외계인");
        }
        System.out.println("입니다");
        int age=0;
        if(input.charAt(0)=='0'){
            age = 2016-(2000+(input.charAt(1)-'0'))+1;
        }else if(input.charAt(0)==1){
            age = 2016-(2000+10+(input.charAt(1)-'0'))+1;
        }else{
            age = 2016-(1900+(input.charAt(0)-'0')*10+(input.charAt(1)-'0'))+1;
        }
        System.out.println("당신의 나이는 "+age+"세 입니다");
        if(age<19){
            System.out.println("미성년자이시네요 담배,술 금지 입니다");
        }
    }
 
}
cs

결과


'* Programming > JAVA' 카테고리의 다른 글

DAY14 Lotto(객체지향)  (0) 2016.07.25
DAY14 StringBuffer  (0) 2016.07.21
DAY13 String 2  (0) 2016.07.21
DAY13 String 1  (0) 2016.07.21
DAY13 학생 성적관리 프로그램  (0) 2016.07.21

+ Recent posts