본문 바로가기

Learning/JAVA

자료형, if문, 다중 if문

  • 자료형 (int, long, float, double, char) (Exam03)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package day01;
public class Exam03 {
     public static void main (String[] args) {
          int a = 135;
          int b = 20;
          System.out.println("덧셈: " +(a+b));
          System.out.println("뺄셈: " +(a-b));
          System.out.println("곱셈: " +(a*b));
          System.out.println("나눗셈: " +(a/b));
          System.out.println("나머지: " +(a%b));
          System.out.println(a+"+"+b+"=" +(a+b));
          System.out.println(a+"%"+b+"=" +(a%b)); //문자열일때 "" 사이에.
          //int는 4바이트 (32비트)
          //long은 8바이트
          long c = 1000000000000L; //정수형은 int가 디폴트.
          
          float f = 3.25f; //4바이트
          double d = 3.25//8바이트. 부동 소수점은 더블이 디폴트임.
          
          //char는 문자 하나
          char ch = 'A';
          //char ch1 = "B"; 오류발생. 따옴표는 문자열을 표현.
    }
}

 

  • 자료형 (원의 넓이 구하기) (Exam04)

 

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
package day01;
public class Exam04 {
     public static void main(String[] args){
          double r = 5;
          final double PI = 3.14//final은 수정 못하도록 고정하는 거. final값은 보통 대문자로 표기.
          System.out.println(r*r*PI);
          
          float f = 5.0f; //4바이트
          int num = 10//4바이트
          f = num;
          //a=b 일때 a와 b는 같은 유형이어야 함. f와 num은 다른 데이터타입.
          //자바에선 정수형보다 부동소수점이 더 큰 형태
          //float = int 형변환 (캐스팅) <-자동형변환
          System.out.println("f:" +f);
          
          // num = f; float형을 작은 int형에 넣을 수는 없음.
     num = (int) f; //int <-float 형변환 (캐스팅) <-명시적형변환
        long num1 = 100L;
        
        f = num1; //float = long
        double area = r*r*PI;
        System.out.println("원넓이:" +area);
        
    }
}

 

  • if 문, 다중 if 문 (Exam05)

 

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
package day01;
public class Exam05 {
    public static void main(String[] args) {
        int a=49;
        //a는 50보다 크다
        if(a>50) { //true
            System.out.println("a는 50보다 크다");
//true일때 실행할 문장
        } else { //false
            System.out.println("a는 50보다 작다"); 
//false일때 실행할 문장
        }
        
        if(a%2==0) { //true라면 2로 나눴을때 나머지 0
            System.out.println(a+" : 짝수");
        } else { //false라면 2로 나눴을때 나머지가 1
            System.out.println(a+" : 홀수");
        }
//다중 if
        int b=252;
        if(b<0) {
            System.out.println("0미만");
        }else if(b<100) { //b가 0보다 작지 않다는 조건을 충족하지 않은 다음에 대조하는 조건. 
자동으로 b가 0과 100사이에 들어간다는 뜻임.
            System.out.println("0에서 99사이 수");
        }else if(b<200) {
            System.out.println("100에서 199사이 수");
        }else if(b<300) {
            System.out.println("200에서 299사이 수");
        }else {
            System.out.println("300 이상 수");
        }
int c=-100;
        if(c<0) {
            System.out.println("0미만");
        }
        if(c>=0 && c<100){
            System.out.println("0에서 99사이 수");
        }
        if(c>=100 && c<200) {
            System.out.println("100에서 200사이 수");
        }
        if(c>=300) {
            System.out.println("300이상 수");
        }
   int su=-142;
        if(su>=0 && su%2==0){
            System.out.println("su는 0보다 크고 짝수다");
        }
        else if(su>=0 && su%2!=0){
            System.out.println("su는 0보다 크고 홀수");
        }   
        else{
            System.out.println("su는 음수다");
        }
    }
}