본문 바로가기
JAVA

JAVA(열거형)_2024-05-27

by 앵보몬 2024. 5. 27.
728x90
반응형
package second;

public class SumAvg {

    public static void main(String[] args) {

        int i = 1;

        int sum = 0;

        for (i = 1; i <= 100; i++) {

            sum += i;

        }

        System.out.println("1~100까지 합계 : " + sum);

        System.out.printf("평균 : %.2f", (double) sum / 100);

    }

}
1~100까지 합계 : 5050
평균 : 50.50

 

package second;

public class SignPrint {

    public static void main(String[] args) {

        int num = -5;

        int num1 = 7;

        int min = 0;

        min = (num < num1) ? num : num1;

        System.out.println("최소값 : " + min);

    }

}
최소값 : -5

 

 

package second;

public class RoundOff {

    public static void main(String[] args) {

        int value = 456;

        System.out.println(value / 100 * 100);

    }

}
400

 

package second;

public class Student {

    static int studentCount = 0;

    String name;

    public static void addStudent() {

        studentCount++;

    }

    public static int getStudenCount() {

        return studentCount;

    }

    public static void main(String[] args) {

        Student.addStudent();

        Student.addStudent();

        Student.addStudent();

        System.out.println("현재 학생 수 : " + Student.getStudenCount());

    }

}
현재 학생 수 : 3

 

package second;

public class CountBox {

    public static void main(String[] args) {

        AppleBasket apbas = new AppleBasket(123);

        apbas.ShowBasket();

    }

}

class AppleBasket {

    int apple;

    int basket;

    public AppleBasket(int apple) {

        this.apple = apple;

        this.basket = apple / 10;

    }

    public void ShowBasket() {

        System.out.println("사과 갯수 : " + apple + "개");

        if (apple % 10 == 0) {

            System.out.println("필요한 바구니 갯수 : " + basket + "개");

        } else {

            System.out.println("필요한 바구니 갯수 : " + (basket + 1) + "개");

        }

    }

}
사과 갯수 : 123개
필요한 바구니 갯수 : 13개

 

 

 

열거형

An은 상수 (변수와 같이 변경할 수 없는 변수 ) enum그룹을 나타내는 특수 "클래스"입니다. final 을 만들려면 클래스나 인터페이스 대신 키워드를 enum사용 하고 상수를 쉼표로 구분하세요. enum대문자여야 합니다. Enum은 "enumerations"의 줄임말로 "구체적으로 나열됨"을 의미합니다.

 

Switch 문의 열거형

switch열거형은 해당 값을 확인하기 위해 명령문 에서 자주 사용됩니다 .

https://www.w3schools.com/java/java_enums.asp

 

728x90
반응형