본문 바로가기
공부하는 모지리

[정보처리기사 실기] JAVA 예상문제 6선.

반응형

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
package Section009;
 
public class Sample01 { //수열의 합  i값과 j값 구하기
    public static void main (String[ ] args) {
        // i++; ++i; i=i+1; i+=1
        //j = j + i; //j+= i
        int i , j=0;
        for (i = 1; i <= 10; i++){ 
            j = j + i;
            System.out.printf ("i = %2d, j = %2d\n", i, j);
        //    System.out.println ("i =" + i + " , "+ "j=" + j);
        }
        System.out.printf ("--------------------\n");
        System.out.printf ("i = %d , j = %d\n", i, j);
    }
}
 
// 동일한 3가지 표현 알아두기 
//for (i = 1; i <= 10; i++)
//for (i = 1; i < 11; i++)
//for (i = 1; i !=11; i++) 비교 :: for(i=10;i>=1;i--) 감소
 
// 결과
 
=  1, j =  1
=  2, j =  3
=  3, j =  6
=  4, j = 10
=  5, j = 15
=  6, j = 21
=  7, j = 28
=  8, j = 36
=  9, j = 45
= 10, j = 55
--------------------
= 11 , j = 55
 
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
while문으로 변경
 
 
        // i++; ++i; i=i+1; i+=1
        //j = j + i; //j+= i
        int i=1 , j=0;
        while(i<=10) {
            j = j + i;
            System.out.printf ("i = %2d, j = %2d\n", i, j);
        //    System.out.println ("i =" + i + " , "+ "j=" + j);
            i++;
        }
        System.out.printf ("--------------------\n");
        System.out.printf ("i = %d , j = %d\n", i, j);
    }
}
cs

구조부터 확인해야한다. while문 안에 i++ 초기 값이 상단에 있을 경우에는
while 문 밖에 i 초기값은 i=0;이 되므로 확인을 해야한다 ( i=1이 아님)
카운트를 하고나서 시작하는지 파악하자.

 

2. 배열 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package Section009;
 
public class Sample02 {
    public static void main(String[ ] args) {
        int a[]= new int[5];
        int i;
        i = 0
        while (i < 5){ // i <= 4 // i != 5
            a[i] = i + 10;
             i++// i++;가 위로 올라가면 i = 0; -> i + -1;로 바뀌고 i<5 -> i<4로 바뀐다)
        }
        System.out.printf("i = %4d", i);
        for (i = 0; i < 5; i++) {
            System.out.printf("%4d", a[i]);    
        }
        }
}
 
// 결과
 
=    5  10  11  12  13  14
 
cs

 

3.  제어 변수 ( i ) , 강화된 for 문   *******

hap = hap + i; *******

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package Section009;
 
public class Sample03 {
    public static void main(String[ ] args) {
        int[ ] a =  {90,100,80,70,60,50,30};
        int hap = 0;
        float avg;
        for (int i : a){
            hap = hap + i;
}
        avg = (float)hap / a.length;
        System.out.printf("%4d, %4.2f", hap, avg);
    }
}
// 결과

 480, 68.57

cs

 

for (i=0;i<a.length;i++){

    hap = hap + a[i];

}

// a 배열 안에 있는 값 합계

 

4. 문자열 뒤집기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package Section009;
 
public class Sample04 {
    public static void main(String[ ] args) {
        String str = "Information!";
        int n = str.length( );
        char[ ] st = new char [n];
        n--;
        for (int k = n; k >= 0; k--)  {
            st[n-k] = str.charAt(k);
        }
        for (char k : st)  {
            System.out.printf("%c", k);
        }
    }
}
 
 
//결과
 
!noitamrofnI 
cs

while문으로 바꾼 예제

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package Section009;
 
public class Sample04 {
    public static void main(String[ ] args) {
        String str = "Information!";
        int n = str.length( );
        char[ ] st = new char [n];
        n--;
        int k = n;
        while ( k >= 0 )  {  // FOR를 while 문으로 바꾼 
            st[n-k] = str.charAt(k);
            k--;    }
        for (char x : st)  {
            System.out.printf("%c", x);
        }
    }
}
 
cs

5. 생성자

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Employee {
    String name;
    int idNum;
    int salary;
    boolean sex;
}
 
public class Sample05 {
    public static void main(String[ ] args) {
        Employee myJik = new Employee( );
        myJik.name = "홍길동";
        myJik.idNum = 17001;
        myJik.salary = 4500000;
        myJik.sex = true;
        System.out.printf("%s\n", myJik.name);
        System.out.printf("%d\n", myJik.idNum);
        System.out.printf("%d\n", myJik.salary);
        System.out.printf("%b\n", myJik.sex);
    }
}
cs

6.

반응형

댓글