본문 바로가기
JAVA/배열

배열 예제(1)

by pms93 2022. 7. 20.
package arrays;

import java.util.Scanner;

public class ArrayQuiz {

	public static void main(String[] args) {

		// Quiz1
		// 국어, 영어, 수학 점수를 입력받고 총점, 평균을 출력
		// 단, 각 과목의 입력 점수가 0~100점 범위에서 벗어날 때 다시 입력하게 하라.
		Scanner sc = new Scanner(System.in);
		String[] sub = { "국어", "영어", "수학" };
		int[] score = new int[4];
		int idx = 0;
		double avg;

		while (idx < score.length - 1) {
			System.out.printf("%s점수 입력 : ", sub[idx]);
			score[idx] = sc.nextInt();

			// 과목점수가 범위 밖인 경우 재입력 받도록 예외처리
			if (score[idx] < 0 || score[idx] > 100) {
				System.out.println("다시 입력하세요");
				continue;
			}

			score[3] += score[idx];
			idx++;
		}

		avg = score[3] / (double) sub.length;

		System.out.printf("총점 : %d, 평균 : %.2f\n", score[3], avg);

		// Quiz2
		// 2022년 기준 n월 n일에 대한 숫자를 입력받아 요일을 출력
		int month = 0, day = 0, totalDate = 0;
		// 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
		int[] dateArray = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		String[] date = { "금", "토", "일", "월", "화", "수", "목" };

		while (true) {
			try {
				System.out.print("몇월? : ");
				month = sc.nextInt();
			} catch (Exception e) {
				System.out.println("숫자를 입력해주세요.");
				sc.nextLine();
				continue;
			}

			while (true) {
				try {
					System.out.print("몇일? : ");
					day = sc.nextInt();
				} catch (Exception e) {
					System.out.println("숫자를 입력해주세요.");
					sc.nextLine();
					continue;
				}
				break;
			}
			break;
		}

		if (month <= 0 || month > 12 || day <= 0 || day > dateArray[month - 1])
			System.out.println("잘못된 날짜 형식입니다.");
		else {
			int tmp = (totalDate + day) % 7;
			System.out.printf("2022년 %02d월 %02d일은 %s요일 입니다.", month, day, date[tmp]);
		}
	}

}

'JAVA > 배열' 카테고리의 다른 글

배열 예제(3)  (0) 2022.07.20
배열 예제(2)  (0) 2022.07.20
Array_다차원배열(3)  (0) 2022.07.20
Array_1차원배열(2)  (0) 2022.07.20
Array_1차원배열(1)  (0) 2022.07.19