(5) Op & IF & Char

JAVA/JAVA 이론 2018. 10. 22. 21:37


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
package Studypackage;
 
public class studyclass 
{
    
    public static void main(String[] args)
    {
        int val = 10;
        
        val = val + 1// 11
        val++;
        ++val;
        
        System.out.println("val = " + val); // 13

        
        System.out.println();
        System.out.println("단항 연산자 테스트");
        
        val = 10;
        System.out.println("val++ = " + val++);
        // println(); 실행 후 -> ++
        System.out.println("val = "+ val);
        System.out.println();
        
        val = 10;
        System.out.println("++val = " + ++val);
        // ++ -> println(); 실행
        System.out.println("val = " + val);
        System.out.println();

  
        int sum = 0;
        val = 10;
        //1)
        sum = sum + val++;        // postfix (후치)
        System.out.println("sum = " + sum); // 10
        sum = 0; val = 10;
        //2)
        sum = sum + ++val;         // prefix (전치)
        System.out.println("sum = " + sum); // 11

        
    }
}
 
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
package Studypackage;
 
 
 
public class studyclass 
{
    
    public static void main(String[] args)
    {
        int sum = 0;
        int i = 5;
        
        sum = sum + i;    // 5
        System.out.println("sum = " + sum);
        
        sum += i;        // sum = sum + i  10
        System.out.println("sum = " + sum);
        
        sum *= i;        // 50
        System.out.println("sum = " + sum);
        
        sum -= i;         // 45
        System.out.println("sum = " + sum);
        
        sum /= i;        // 9
        System.out.println("sum = " + sum);
        System.out.println();


        sum *= i++;        // sum *= ++i; 비교
        System.out.println("sum *= i++ sum = " + sum);
        System.out.println("sum *= i++ i = " + i);
        System.out.println();


        sum *= --i;        // sum *= --i; 비교
        System.out.println("sum *= --i sum = " + sum);    // 45 * 5
        System.out.println("sum *= --i sum = " + i);    // 5 
        System.out.println();


        //나머지 연산자
        System.out.println("6 % 5 : " + (6%5));        //1
        System.out.println("7 % 5 : " + (7%5));        //2
        System.out.println("8 % 5 : " + (8%5));        //3
        System.out.println("9 % 5 : " + (9%5));        //4
        System.out.println("10 % 5 : " + (10%5));     //0


    }
}
 
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
package Studypackage;
 
public class studyclass 
{
    /*
     *  문자열 : "" 기호 , 문자 0개 이상의 집합   %s -> String 클래스
     *  문자    : '' 기호 , 반드시 1개 문자 포함   %c -> char
     */
    public static void main(String[] args)
    {
        System.out.println("");
        //System.out.println('');    // 오류 , 반드시 1개
        
        System.out.println('a');
        System.out.printf("%c - %c\n\n",'A','가');

        
        char ch1 ='a';
        int ch2 ='a';        // 문자 -> 정수형으로 casting (코드 값)
        
        System.out.printf("문자와 코드 값 출력\n");
        //System.out.printf("%c - %d\n",ch1,ch1); // 오류
        System.out.printf("%c - %d\n",ch2,ch2);
        // ch1 = ch1 + 1;    // 문자형은 기본연산 못함
        
        ch2 = ch2 +1;
        System.out.printf("%c - %d\n",ch2,ch2);
        
        ch2 = '가';        // \uac00 (유니코드 값)
        System.out.printf("%c - %d - %x\n",ch2,ch2,ch2);
        
        ch2 = '\ud7a3';
        System.out.printf("%c - %d -%x\n",ch2,ch2,ch2);
        

        /*
         *    - %x - 정수의 16 진수 표기
         *    - 2진수 3자리씩 -> 8진수
         *    - 2진수 4자리씩 -> 16진수
         *    - 2진수 자리수를 줄여서 표현하기 위해 8진수 또는 16진수 사용
         */
    }
}
 
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
package Studypackage;
 
public class studyclass 
{
    /*
     *    if 문 
     *        - 조건식 ( 참 또는 거짓 결과 ) 결과에 따라 실행 명령 선택
     *    형식 1)    
     *        - if(조건식) {조건식이 참일때 실행하는 명령들}
     *
     *    형식2)
     *        - if(조건식) {조건식이 참일때 실행하는 명령들}
     *        - else {조건식이 거짓일때 실행하는 명령들}
     */
    
    public static void main(String[] args)
    {
        int age = 35;
        
        System.out.println("if 형식 1)");
        if(age > 20)
        {
            System.out.println("성인 인증 되었습니다");
        }
        else
        {
            System.out.println("프로그램 종료 !!");
        }


        int score = 34;
        System.out.println("if 형식 2)");
        if(score > 60)
        {
            System.out.println("Pass!!");
        }
        else
        {
            System.out.println("좀더 열심히");
        }


    }
}
 
 
cs


'JAVA > JAVA 이론' 카테고리의 다른 글

(7) While & Do ~ While & Continue  (0) 2018.10.24
(6) If & For  (0) 2018.10.23
(4) String & Boolean  (0) 2018.10.21
(3) KeyInput & FloatingPointNum  (0) 2018.10.18
(2) Java_basic1 - Data Type  (0) 2018.10.17

설정

트랙백

댓글