<--애드센스-->

 프로그래밍 수업이 어제 시작됬다. 교수님이 내가 면접볼 당시 조사했었던 船橋(후나바시)교수님이셨는데, 수업방식이 맘에든다. 매주 과제가 있는데 그것이 여간 귀찮은 것이 아니다. 그러나, 마지막 발전문제 하나만 제출하면 나머지 귀찮은 과제들 안해도 된다는 것! 현명하다.(아직은 그렇게 생각한다...)


 잡담이 길어졌는데, 그래서 첫 수업의 과제는 문자열을 입력하고, 숫자를 입력해서 하나의 키를 만든다. 그 후, 키에 해당하는 숫자를 입력하면 다시 문자열을 출력할 수 있게하는 프로그램을 만들라는 것이다.

 

 대략적인 프로그램의 흐름은 이와같다. 

무한반복{(문자열입력받음)>(숫자입력받음)>(문자열과 숫자 저장)} 

>무한반복{(숫자입력받음)>(숫자에 맞는 문자열 출력)}

 이 프로그램에서 중요한 것은 키가 중복되는 지, 숫자만 입력받아야 할 키에 문자를 입력하진 않았는 지 등을 판단하고, 다시 입력받게 하는 작업이었다.

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package report1;
import java.util.Scanner;
 
class report1{
    static Scanner stdIn = new Scanner(System.in);
    static int pwNumber = 0;
    private static String[][] passwords = new String[100][2];//문자열과 키를 저장할 배열
 
    public static void main(String[] args){//메인 메소드
        boolean input = true;
        boolean output = true;
        String message;
        String key;
        System.out.println("--------[Step 1]--------");
        while(input){//roop of input
            message = getMessage();
            if(message.equals("x"))//X 입력 시 종료.
                input=false;
            else {
                key = getKey();
                encryption(message, key, pwNumber);
                pwNumber++;
            }
        }
        System.out.println("--------[Step 2]--------");
        while(output){//roop of output
            String arg2 = translate();
            if(arg2.equals("x")) output=false;
        }
    }
    
    
    static String translate() {//키에 해당하는 번호를 찾아서 문자열을 반환.
        System.out.println("Put the keyNumber. If you want to escape, put the key 'x'.");
       
        String password = stdIn.nextLine();
        if(checkNum(password)) {
            for(int i = 0; i< pwNumber;i++){
                if(passwords[i][1].equals(password)){
                    System.out.println(passwords[i][0]);
                    break;
                }
                if(i == pwNumber-1 && !passwords[i][1].equals(password)){
                    System.out.println("Wrong key. Please try again.");
                }
            }
        }
        else if(!password.equals("x"))
            System.out.println("The key must to be Number! try again.");
        
        return password;
    }
    static void encryption(String m, String k,int i) {//문자열과 키를 배열에 저장
        passwords[i][0= m;
        passwords[i][1= k;
        System.out.println("Success to create key!");
    }
    static String getMessage(){//문자열을 입력받음.
        System.out.println("Type your message. If you want to stop, type 'x'.");
        String m = stdIn.nextLine();
        while(checkNum(m)) {
            System.out.println("The key must to be English! try again.");
            m = stdIn.nextLine();
        }
        return m;
    }
    static String getKey() {//숫자를 입력받음.
        System.out.println("put your key");
        String k = stdIn.nextLine();
        while(!checkNum(k)) {//숫자인지 검사
            System.out.println("The key must to be Number! Please try again.");
            k = stdIn.nextLine();
        }
        while(checkOverlap(k,passwords)) {//중복이 아닌지 검사
            System.out.println("This key has been used. Please try again.");
            k = stdIn.nextLine();
        }
        
        return k;
    }
    static boolean checkOverlap(String s, String[][] S) {//키의 중복을 체크
        for(int i=0;i<pwNumber;i++) {    
            if(s.equals(S[i][1])) return true;
        }
        return false;
    }
    static boolean checkNum(String k) {//문자열이 숫자인지 아닌지 판별
        boolean isNumber = true;
        try {
            Integer.parseInt(k);
        } catch (Exception e) {
        isNumber = false;
        }
        return isNumber;
    }
}
cs


+ Recent posts