기록해! 정리해!

자바 -열거 본문

JAVA

자바 -열거

zsuling 2022. 7. 11. 17:58

열거: 상수로 정의되어있는 값들

 

 

 

1. Enum(열거) 파일 생성

2. BooleanT

package p171;

public enum BooleanT {
  True,
  False
}

3. MainTest

package p171;

public class MainTest {

	public static void main(String[] args) {

	BooleanT t = BooleanT.True;
	String st = t.toString(); //BooleanT를 String으로 형변환
	if(st.equals("True")) {
		System.out.println("참을 선택하였습니다.");
	}else {
		System.out.println("거짓을 선택하였습니다.");
	}
	}

}

 

º 열거상수는 모두 대문자로 작성한다

 

1. Week

package p171;

public enum Week {
  MONDAY, TUESDAY, WEDNESDAY, THURSDAY, SUNDAY
}

2. MainTest

package p171;

public class MainTest {

	public static void main(String[] args) {

	Week w1 = Week.THURSDAY;
	Week w2 = Week.THURSDAY;
	
	
	if( w1 == w2) {
		System.out.println("같다");
	}else {
		System.out.println("다르다");
	}
	}

}

왜 에러남?

 

 2-1) MainTest (클래스:Calendar)

package p171;

import java.util.Calendar;

public class MainTest {

	public static void main(String[] args) {

	 Calendar cal = Calendar.getInstance();
	 int week = cal.get(Calendar.DAY_OF_WEEK);
	 System.out.println(week);
	 
	 Week today = null;
	 
	 switch(week) {
	 case 1 : 
		 today = Week.SUNDAY;
		 break;
	 case 2 :
		 today = Week.MONDAY;
		 break;
	 case 3 :
		 
	 case 4 :
	 case 5 :
		
	 case 6 :
	 case 7 :
		 
	 }
	 System.out.println(today + "요일입니다.");
	}

}

 2-2

package p171;

import java.util.Calendar;

public class MainTest {

	public static void main(String[] args) {

	 Calendar now = Calendar.getInstance();
	 int year = now.get(Calendar.YEAR);
	 int month = now.get(Calendar.MONTH)+1;
	 int day = now.get(Calendar.DAY_OF_MONTH);
	 System.out.println(year + "년" + month +"월" + day +"일");
	}

}

2-3)

package p171;

import java.util.Calendar;

public class MainTest {

	public static void main(String[] args) {

	 Calendar now = Calendar.getInstance();
	 int year = now.get(Calendar.YEAR);
	 int month = now.get(Calendar.MONTH)+1;
	 int day = now.get(Calendar.DAY_OF_MONTH);
	 System.out.println(year + "년" + month +"월" + day +"일");
	 
	 Week [] days = Week.values();
	 for (Week d:days) {
		 System.out.print(d + " ");
	 }
	}

}

º 확인문제 6번

public class MainTest {

	public static void main(String[] args) {

	int[][] array = {
			{95,86},{83,92,96},{78,83,93,87,88}
	  };
	System.out.println(array.length);
    System.out.println(array[2].length);

여기서 

-1. 전체배열 출력하기

for(int i=0; i<array.length; i++) {
        for(int j=0; j<array[i].length; j++) {
           System.out.print(array[i][j] + " ");
        }
        System.out.println();
        }

-2.  83,92,96만 찍어보기 (array[1][j]출력)

 for(int i=1; i==1; i++) {
        for(int j=0; j<array[i].length; j++) {
           System.out.print(array[i][j] + " ");
        }
        System.out.println();

-3. 95,83,78 만 찍어보기 (array[0][j]출력)

 for(int i1=0; i1<array.length; i1++) {
        for(int j=0; j == 0; j++) {
           System.out.print(array[i1][j] + " ");
        }
        System.out.println();

º 확인문제 7번 : 최대값을 구해보시오

package p171;


public class MainTest {

	public static void main(String[] args) {

	int max =0;
	int [] array = {1, 5, 3, 8 ,2};
	
	
	for(int a:array) {
		if(max <= a) {
		 max = a;	
		}
	}
	System.out.println("MAX:" + max);
}
}

- 최소값 구하기

	for (int a:array) {
		if (min >=a) {
			min = a;
		}
	}
	System.out.println("MIN:" + min);
}
for (int i=0; i<array.length; i++) {
	System.out.print(array[i] + " ");
if(array[i] <= min) {
	min = array[i];
}
}
System.out.println( "\n최소값:" + min);
 }

}

-합/평균 구하기

package p171;


public class MainTest {

	public static void main(String[] args) {

	int[] array = {10,-7,5,3,0,2,4};
	
	int sum = 0;
	double avg = 0.0;
	double ct = array.length;
	
for (int i=0; i<array.length; i++) {
  sum += array[i];
}
 avg = (double)sum/ ct;

System.out.println("누적합 " +sum);
System.out.println("평균 " +avg);
	}
}

 

º 확인문제 8번; 

package p171;


public class MainTest {

	public static void main(String[] args) {
		
	
		int [][] array = {
				{95,86},
				{83, 92, 96},
				{78, 83, 93, 87, 88}
		};
		
		int sum =0;
	    double avg = 0.0;
	    int ct =0;
	    
	    for (int i =0; i<array.length; i++){
	    for (int j =0; j<array[i].length; j++){
	    System.out.print(array[i][j] + " ");
	    sum += array[i][j];
	    ct++;
	    }
	    System.out.println();
	    }
	    avg = (double)sum / ct; 
	    
	    System.out.println("ct:값" + ct);
        System.out.println("배열값 합계: " +sum);
        System.out.println("배열값 평균: " +avg);
	    }

ºOCAJP 배열 문제 - 137번

		int n[][] = { {1,3}, {2,4} };
		for (int i = n.length -1; i>=0; i--) {
			for (int j = n[i].length -1 ; j >= 0; j--) {
				System.out.print(n[i][j]);
			}
		  }

답 : 4231

 

ºOCAJP 배열 문제 - 157번

답 : B

 

ºOCAJP 배열 문제 - 164번

답: C

 

ºOCAJP 배열 문제 - 194번

public static void main(String[] args) {
	
    코드 작성
		
	array[0]=10;
	array[1]=20;
	System.out.println(array[0]+":"+array[1]);

답 : B

 

ºOCAJP 배열 문제 - 195번

		String shirts[][] = new String[2][2];
		shirts[0][0]="red";
		shirts[0][1]="blue";
		shirts[1][0]="small";
		shirts[1][1]="medium";

답: B

 

ºOCAJP 배열 문제 - 206번

int a[]= {1,2,3,4,5};
				for( int e=0; e<5; e+=2) {
					System.out.print(a[e]);
				}

ºOCAJP 배열 문제 - 223번

int[]arr = {1,2,3,4};
		int i = 0;
		do {
			System.out.println(arr[i]+" ");
			i++;
		}while (i< arr.length +1);
		}

답 : A

 

 

 

 

 

'JAVA' 카테고리의 다른 글

자바 - 정적필드, 문제 , 싱글톤, final  (0) 2022.07.13
자바 - 객체  (0) 2022.07.12
자바 - 참조타입 (배열)  (0) 2022.07.08
자바 - board 인터페이스  (0) 2022.07.07
자바 - TV 인터페이스  (0) 2022.07.07
Comments