Day 4 (22.12.29)

Jane·2022년 12월 29일
0

IT 수업 정리

목록 보기
4/124

1. 총점, 평균 구하기

public class Test2 {

	public static void main(String[] args) {
		int kor, eng, math, total;
		kor = 75;
		eng = 60;
		math = 100;
		total = kor + eng + math;
		
        //평균
		double average = total / 3.0;
		
		System.out.println("total : " + total + ", average : " + average);

	}

}
  • total / 3 = 정수 출력
  • total / 3.0 = 실수 출력

2. CharTypeUnicode.java


public class CharTypeUnicode {

	public static void main(String[] args) {
		char ch1 = '헐';
		char ch2 = '확';
		char ch3 = 54736;
		char ch4 = 54869;
		char ch5 = 0xD5D0;
		char ch6 = 0xD655;
		System.out.println(ch1 + " " + ch2);
		System.out.println(ch3 + " " + ch4);
		System.out.println(ch5 + " " + ch6);
	}

}

[Console]
헐 확
헐 확
헐 확

3. boolean

public class Boolean {

	public static void main(String[] args) {
		boolean b1 = true;
		boolean b2 = false;
		System.out.println(b1);
		System.out.println(b2);
		
		int num1 = 10;
		int num2 = 20;
		System.out.println(num1 < num2);
		System.out.println(num1 > num2);
	}

}

[Console]
true
false
true
false

4. 상수 (Constants)

  • 상수 : 수식에서 변하지 않는 값
  • final 선언
  • 상수는 대문자로 짓는 것이 관례, 둘 이상의 단어는 언더바로 연결
  • 초기화(initial) : 객체나 변수의 초기값을 할당하는 것
public class Constants {

	public static void main(String[] args) {
		final int MAX_SIZE = 100;
		final char CONST_CHAR = '상';
		final int CONST_ASSIGNED;
		CONST_ASSIGNED = 12;
		
		System.out.println("상수1 : " + MAX_SIZE);
		System.out.println("상수2 : " + CONST_CHAR);
		System.out.println("상수3 : " + CONST_ASSIGNED);
	}

}

[Console]
상수1 : 100
상수2 : 상
상수3 : 12

5. 원의 넓이 구하기

public class RoundArea {

	public static void main(String[] args) {
		final double PI = 3.14;
		double r = 10;
		double round = PI * r * r;
		
		System.out.println("원의 넓이 : " + round);
	}

}
  • PI는 변하지 않는 수이므로 상수로 선언한다.
  • 반지름, 넓이는 변하는 수이므로 변수로 선언한다.
profile
velog, GitHub, Notion 등에 작업물을 정리하고 있습니다.

0개의 댓글