반응형
- 7번: 주어진 배열의 항목에서 최대값을 구하시오.
package ch5;
public class exercise7 {
public static void main(String[] args) {
int max = 0;
int[] array = { 1, 5, 3, 8, 2 };
for (int i = 0; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
System.out.println(max);
}
}
- 8번: 주어진 배열의 전체 항목의 합과 평균값을 구하시오.
package ch5;
public class exercise8 {
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;
double count = 0.0;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
sum += array[i][j];
}
count += array[i].length;
}
avg = sum / count;
System.out.println(sum);
System.out.println(avg);
}
}
- 9번: 코드를 완성하시오.
package ch5;
import java.util.Scanner;
public class exercise9 {
public static void main(String[] args) {
boolean run = true;
int studentNum = 0;
int[] scores = null;
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 = scanner.nextInt();
if (selectNo == 1) {
System.out.print("학생수> ");
studentNum = scanner.nextInt();
scores = new int[studentNum];
} else if (selectNo == 2) {
for (int i = 0; i < studentNum; i++) {
System.out.print("scores[" + i + "]> ");
scores[i] = scanner.nextInt();
}
} else if (selectNo == 3) {
for (int j = 0; j < studentNum; j++) {
System.out.println("scores[" + j + "]> " + scores[j]);
}
} else if (selectNo == 4) {
int highst = 0;
double sum = 0.0;
double avg = 0.0;
for (int k = 0; k < studentNum; k++) {
if (scores[k] > highst) {
highst = scores[k];
}
}
for (int k = 0; k < studentNum; k++) {
sum += scores[k];
}
avg = sum / studentNum;
System.out.println("최고점수: " + highst);
System.out.println("평균점수: " + avg);
} else if (selectNo == 5) {
run = false;
}
}
System.out.println("프로그램 종료");
}
}
728x90
반응형
'Java > 이것이 자바다' 카테고리의 다른 글
상속 (0) | 2023.03.08 |
---|---|
이자바 6장(클래스) 확인문제 (0) | 2022.11.24 |
이자바 4장(조건문과 반복문) 확인문제 (0) | 2022.11.23 |
어노테이션 (0) | 2022.10.21 |
클래스 (2) | 2022.10.13 |
댓글