출처: https://bumcrush.tistory.com/182 [맑음때때로 여름]
ex) float + int = float + float

 

* byte + byte = int + int -> int

 

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
        /*
                                             우선순위
                                            
            boolean        1byte                낮음
                                          
           byte        1byte              
           short        2byte            
           int            4byte              
           long        8byte                   
                                           
           float        4byte               
            double        8byte                높음
            
            자료형의 변환 (auto, cast)
            
         */
        
        
        
        // 자동 자료형 변환 (auto)
        
        short sh = 12345;    // 2byte
        int i;                // 4byte
        
        i = sh; //자동형변환
        System.out.println("i = " + i);
        
        
        
        
        // 강제형 변환 (cast)
        
        i = 1234567891;
        
        // sh = i; 에러남!
        
        sh = (short) i; 
        System.out.println("sh = "+ sh);  
        // sh=723이 나온다.
        // 그러므로 형변환 할 때는 범위의 값을 잘 생각해서 변환해야한다.
        
        
        
        
        
        long l = 123456789L;
        float f = l;
        double d = l;
        
        System.out.println("f = " + f); // float라서 용량부족으로 여섯번째 숫자부터 에러남
        System.out.println("d = " + d); // 1.2345678E8
        
        //1.23456789E8
        //1.23456789 * 10^8
        
        double d1 = 1.234e2// 123.4 
        double d2 = 1.234e-2// 0.01234
        
        int n1, n2;
        n1=3;
        n2=2;
        
        double result = n1 / n2 ;  // 1.5가 나오지 않는다.
        double result2 = (double)n1/n2; 
        
        System.out.println(result); // 1.0 틀림
        System.out.println(result2); // 1.5 맞음
        
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
         int a; // =3240; // 지불해야할 금액
         int b; //=10000; // 본인이 낸 금액
         
         Scanner money=new Scanner(System.in);
         System.out.print("지불해야할 금액 : ");
         a=money.nextInt();
         System.out.print("낸 금액 : ");
         b=money.nextInt();
         
         
         int c; // 거스름돈
         c=b-a; 
    
         int d,e,f,g,h,i; // 5000원 1000원 500원 100원 50원 10원의 갯수
         
         
         
        /* d=c/5000; // 5000원권의 갯수
         e=(c-(d*5000))/1000; // 1000원권의 갯수
         f=(c-(d*5000)-(e*1000))/500 ;// 500원 갯수
         g=(c-(d*5000)-(e*1000)-(f*500))/100 ; // 100원 갯수
         h=(c-(d*5000)-(e*1000)-(f*500)-(g*100))/50; // 50원 갯수
         i=(c-(d*5000)-(e*1000)-(f*500)-(g*100)-(h*50))/10; // 10원 갯수 */
         
         d=c/5000;
         e=(c%5000)/1000;
         f=(c%1000)/500;
         g=(c%500)/100;
         h=(c%100)/50;
         i=(c%50)/10;
         
         
         System.out.println("5000원 갯수 = " + d);
         System.out.println("1000원 갯수 = " + e);
         System.out.println("500원 갯수 = " + f);
         System.out.println("100원 갯수 = " + g);
         System.out.println("50원 갯수 = " + h);
         System.out.println("10원 갯수 = " + i); 
    
         
cs

//삼항 연산자
// (조건)?"값1":"값2";  
// 조건이 참이면 값1을, 거짓이면 값2를 대입한다

int number = 0 ;
int i;

i= (number<5)?100:200;                  = 100

 

String str;

str= (number>0)? "양수입니다":"0이거나음수입니다"               = 0이거나음수입니다

 

 

 

 

&&

AND 그리고, 두가지가 모두 참인경우 -> true


  ||

OR 또는, 두가지 중에 하나만 참이거나 둘 다 참인 경우 -> true


  ! 

~아닌,NOT

>,<,==

크고 작고 같고~
>=, <=

크거나 같고 작거나같고~ 

a != b

a와 b가 같지않다

 

!(num<0) → num>=0

!(num>0 || num<10)) num<=0 && num>=10

 

+ Recent posts