** 8월 4주차 내용 배운부분 꼭 복습하기. **
1. enum
- enum은 enumerated type이며 프로그래밍에서 관련 있는 상수들의 집합
- 다양한 상수값을 나타냄.
- 코드의 가독성과 유지보수 향상
public enum enum이름 {
VALUE1,
VALUE2,
VALUE3
}
- enum을 사용하기 위해서 변수처럼 선언하고 enum이름.원하는값 기술
EnumName value = EnumName.VALUE1;
switch(value) {
case VALUE1:
case VALUE2:
break;
}
(1) enum 예제(실습)
enum Season {
SPRING,SUMMER,AUTUMN,WINTER
}
public class SeasonalActivities {
public static void main(String[] args) {
Season currentSeason = Season.AUTUMN;
switch(currentSeason) {
case SPRING:
System.out.println("꽃 구경하기");
break;
case SUMMER:
System.out.println("해수욕");
break;
case AUTUMN:
System.out.println("단풍 구경하기");
break;
case WINTER:
System.out.println("눈싸움");
break;
}
}
}
enum Week {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
}
public class Weeks {
public static void main(String[] args) {
Week today = Week.MONDAY;
switch(today) {
case MONDAY:
System.out.println("학원 수업듣기");
break;
case TUESDAY:
System.out.println("학원 수업듣기");
break;
case WEDNESDAY:
System.out.println("학원 수업듣기");
break;
case THURSDAY:
System.out.println("학원 수업듣기");
break;
case FRIDAY:
System.out.println("학원 수업듣기");
break;
case SATURDAY:
System.out.println("복습하기");
break;
case SUNDAY:
System.out.println("휴식하기");
break;
}
}
}
(2) 은행 프로그램 추가문제
- 배열을 이용해서 n명의 사용자 계정을 관리하는 프로그램으로 업그레이드
- n은 복습할때 임의로 정해서 복습해보기.
- 메뉴 변경: 1. 입금 2. 출금 3. 조회 4. 작업계정선택(0~n) 5. 전체계정선택 0. 종료
- 계정 인덱스 저장을 위해서 int nowUserIndex=0으로 선언한다.
String input = "";
Scanner scanner = new Scanner(System.in);
double account[] = new double[100];
int nowUserIndex = 0;
while(!input.equals("0")) {
System.out.println("---------------------------------------------------------------");
System.out.println("1. 입금 | 2. 출금 | 3. 조회 | 4. 작업계정 선택 | 5, 전체계정 선택 | 0.종료");
System.out.println("---------------------------------------------------------------");
System.out.print("선택> ");
input = new Scanner(System.in).nextLine();
if(input.equals("1")) {
System.out.printf("%d계정의 입금 서비스에 접속하셨습니다."+"\n", nowUserIndex);
System.out.print("입금액> ");
Scanner input1 = new Scanner(System.in);
double deposit = Double.parseDouble(input1.nextLine());
if(deposit>0) {
account[nowUserIndex] += deposit;
System.out.printf("%d계정에 입금이 완료되었습니다. 현재 잔고는 %.1f원입니다."+"\n", nowUserIndex, account[nowUserIndex]);
} else {
System.out.println("정보가 올바르지 않습니다. 다시 입력해주세요");
}
} else if (input.equals("2")) {
System.out.printf("%d계정의 출금 서비스에 접속하셨습니다."+"\n", nowUserIndex);
System.out.print("출금액> ");
Scanner input2 = new Scanner(System.in);
double withdrawal = Double.parseDouble(input2.nextLine());
if(account[nowUserIndex]>=withdrawal) {
account[nowUserIndex] -= withdrawal;
System.out.printf("%d계정에 출금이 완료되었습니다. 현재 잔고는 %.1f원입니다."+"\n", nowUserIndex, account[nowUserIndex]);
} else {
System.out.println("정보가 올바르지 않습니다. 다시 입력해주세요");
}
} else if(input.equals("3")) {
System.out.print("조회> ");
System.out.printf("%d계정의 조회 서비스에 접속하셨습니다. 잔액은 %.1f원입니다."+"\n", nowUserIndex, account[nowUserIndex]);
} else if(input.equals("4")) {
System.out.printf("%d계정의 작업계정 서비스에 접속하셨습니다."+"\n", nowUserIndex);
System.out.print("계정을 변경하고 싶다면 0~99 사이의 숫자를 선택해주세요> ");
Scanner input3 = new Scanner(System.in);
int login = Integer.parseInt(input3.nextLine());
if(login>=0 && login<100) {
nowUserIndex = login;
} else {
System.out.println("존재하지 않는 계정입니다");
}
} else if(input.equals("5")) {
System.out.println("은행의 전체계정이 조회됩니다.");
for(int i=0; i<account.length; i++) {
System.out.println(i + " : " + account[i] + "원");
}
} else {
System.out.println("은행 관리 서비스가 종료되었습니다. 이용해주셔서 감사합니다.");
break;
}
}
2. 이것이 자바다(복습)
(1) 예제문제
- 학생들의 점수를 분석하는 프로그램. while문과 Scanner의 nextLIne()메소드 활용하기
boolean run = true;
int[] scores = new int[10];
int studentNum = 0;
Scanner scanner = new Scanner(System.in);
while(run) {
System.out.println("---------------------------------------------------");
System.out.println("1. 학생수 | 2. 점수입력 | 3. 점수리스트 | 4. 분석 | 5. 종료");
System.out.println("---------------------------------------------------");
System.out.print("선택> " );
int selectNo = Integer.parseInt(scanner.nextLine());
if(selectNo == 1) {
System.out.print("학생수> ");
studentNum = Integer.parseInt(scanner.nextLine());
scores = new int[studentNum];
} else if(selectNo == 2) {
for(int i=0; i<scores.length; i++) {
System.out.print("scores[" + i + "]> ");
scores[i] = Integer.parseInt(scanner.nextLine());
}
} else if(selectNo == 3) {
for(int i=0; i<scores.length; i++) {
System.out.println("scores[" + i + "]: " + scores[i]);
}
} else if(selectNo == 4) {
int max = 0;
int sum = 0;
double avg = 0;
for(int i=0; i<scores.length; i++) {
max = (max<scores[i]) ? scores[i] : max;
sum += scores[i];
}
avg = (double) sum / studentNum;
System.out.println("최고점수: " + max);
System.out.println("평균점수: " + avg);
} else {
run = false;
System.out.println("프로그램을 종료합니다.");
}
}
int[] array = {1,5,3,8,2};
int max=0;
for(int i=0; i<array.length; i++) {
if(max < array[i]) {
max = array[i];
}
System.out.println("max: " + max);
}
- 주어진 배열 항목의 전체 합과 평균을 구해 출력하는 코드
int[][] array = {
{95, 86},
{83, 92, 96},
{78, 83, 93, 87, 88}
};
int sum = 0;
double avg = 0;
int count = 0;
for(int i=0; i<array.length; i++) {
for(int j=0; j<array[i].length; j++) {
sum += array[i][j];
count++;
}
}
avg = (double) sum / count;
System.out.println("sum: " + sum);
System.out.println("avg: " + avg);