728x90
반응형
📂프로젝트 파일
💾소스코드
import java.util.Arrays;
import java.util.Scanner;
public class Ch5_array2 {
public static void main(String[] args) {
func7();
}
/*---------2022.06.22 Quiz----------*/
// 2. 학생들의 점수를 저장할 정수 배열을 100, 88, 100, 100, 90 로 초기화합니다.
// 이 배열의 요소 값을 모두 더한 총합과 평균을 반복문을 사용해서 구하고 출력하세요
static void func2() {
int[] score = {100, 88, 100, 100, 90};
int sum = 0;
float average = 0.0f;
for(int i=0; i<score.length; i++) {
sum += score[i];
}
System.out.println("총합 : " + sum);
average = (float)sum / score.length;
System.out.println("평균 : " + average);
}
// 3. 길이가 5인 정수형 배열에 사용자로부터 값을 입력받아 초기화하고 이 중 가장 큰 값을 출력
static void func3() {
int[] arr = new int[5];
int max = 0, min = 999;
System.out.print("값을 입력하세요>>");
Scanner s = new Scanner(System.in);
for(int i=0; i<arr.length; i++) {
arr[i] = s.nextInt();
if(arr[i] > max) {max = arr[i];}
if(arr[i] < min) {min = arr[i];}
}
System.out.println("가장 큰 값 : " + max);
System.out.println("가장 작은 값 : " + min);
}
// 4. 정수 길이가 10인 정수형 배열을 만들고 1~45 난수를 저장하고 출력하세요(중복허용)
static void func4() {
int[] arr = new int[10];
for(int i=0; i<arr.length; i++) {
arr[i] = (int)(Math.random()*45) + 1;
}
System.out.println(Arrays.toString(arr));
}
// 6. 1~10까지 중복되지 않은 정수 10개를 출력하세요
static void func6() {
int[] arr = new int[10];
int temp;
for(int i=0; i<arr.length; i++) {
arr[i] = i+1;
}
for(int i=0; i<arr.length*3; i++) {
int r = (int)(Math.random()*arr.length);
// 첫 번째 값 고정하고 비교하면서 나머지 섞기
temp = arr[r];
arr[r] = arr[0];
arr[0] = temp;
}
System.out.println(Arrays.toString(arr));
}
// 7. 로또번호 발생기 - 1~45까지의 수 6개 출력
static void func7() {
int[] lotto = new int[6];
int temp = 0;
for(int i=0; i<lotto.length; i++) {
lotto[i] = (int)(Math.random()*45) + 1;
}
for(int i=0; i<lotto.length; i++) {
for(int j=i; j<lotto.length; j++) {
if(lotto[i] > lotto[j]) {
temp = lotto[i];
lotto[i] = lotto[j];
lotto[j] = temp;
}
}
}
System.out.println("오늘의 로또 번호!");
System.out.println(Arrays.toString(lotto));
}
}
💾소스코드
import java.util.Arrays;
import java.util.Scanner;
public class Ch5_array3 {
public static void main(String[] args) {
func8();
}
/*------------다차원 배열-------------*/
// 6. 다음과 같이 2차원 배열로 초기화하고 2차원 형태로 출력하시오.
// { {100, 100, 100},
// {20, 20, 20},
// {30, 30, 30},
// {40, 40, 40},
// {50, 50, 50} };
static void func6() {
int[][] score = { {100, 100, 100},
{20, 20, 20},
{30, 30, 30},
{40, 40, 40},
{50, 50, 50} };
for(int i=0; i<5; i++) {
for(int j=0; j<3; j++) {
System.out.print(score[i][j] + ", ");
}
System.out.println();
}
}
// 7. 3명의 학생 대상 C 시험과 Java 시험을 봐서 다음과 같다.
// 90, 95
// 85, 100
// 95, 90
// 7-1. 이들 점수로 2차원 배열을 초기화하고 2차원 형태로 출력
// 7-2. 3명의 C언어 점수 평균을 반복문으로 구해서 출력
static void func7() {
int[][] score = { {90, 95},
{85, 100},
{95, 90} };
int sum = 0;
float average = 0.0f;
for(int i=0; i<score.length; i++) {
for(int j=0; j<score[i].length; j++) {
// 모든 점수 출력하기
System.out.print(score[i][j] + ", ");
}
System.out.println();
}
for(int i=0; i<score.length; i++) {
sum += score[i][0];
}
average = sum / score.length;
System.out.println("3명의 C언어 평균 점수 : " + average);
}
// 8. 다섯 개의 매장을 가지고 있고 각 매장에 3명의 직원(과장, 대리, 사원)이 있다.
// 전 직원의 급여 액수를 저장하기 위한 5x3 배열을 만들고
// 8-1. 사용자로부터 급여액을 입력받아 배열에 저장한다.
// 8-2. 매장별 총 급여를 구해서 출력
// 8-3. 직급별 총 급여를 구해서 출력
static void func8() {
int[][] money = new int[5][3];
int[] sum_job = new int[3];
int[] sum_store = new int[5];
System.out.println("급여액을 입력하세요>>");
Scanner s = new Scanner(System.in);
for(int i=0; i<money.length; i++) {
for(int j=0; j<money[i].length; j++) {
// 요소들의 값은 정수로 받는다.
money[i][j] = s.nextInt();
}
}
System.out.println("\n[ 모든 급여액 ]");
for(int i=0; i<money.length; i++) { // 0,1,2,3,4
for(int j=0; j<money[i].length; j++) { // 0,1,2,3
// 모든 급여액 출력하기
System.out.print(money[i][j] + ", ");
}
System.out.println();
}
System.out.println("\n[ 매장별 총 급여 ]");
// 매장별 총 급여, sum_store[0~4](0,1,2,3,4)
// 첫 번째 매장 : money[0][0] + money[0][1] + money[0][2]
// 두 번쩨 매장 : money[1][0] + money[1][1] + money[1][2]...
for(int i=0; i<money.length; i++) {
for(int j=0; j<money[i].length; j++) {
sum_store[i] += money[i][j];
}
}
for(int i=0; i<money.length; i++) {
System.out.println( (i+1) + "번째 매장 총 급여 : " + sum_store[i] );
}
System.out.println("\n[ 직급별 총 급여 ]");
// 직급별 총 급여, sum_job[0~2](0,1,2)
// 과장 : money[0][0] + money[1][0] + money[2][0]...
// 대리 : money[0][1] + money[1][1] + money[2][1]...
// 사원 : money[0][2] + money[1][2] + money[2][2]...
for(int i=0; i<money[0].length; i++) {
for(int j=0; j<money.length; j++) {
sum_job[i] += money[j][i];
}
}
System.out.println("과장 총 급여 : " + sum_job[0]);
System.out.println("대리 총 급여 : " + sum_job[1]);
System.out.println("사원 총 급여 : " + sum_job[2]);
// System.out.println("직급별 총 급여 : " + sum_job);
}
}
728x90
반응형
'Language > JAVA' 카테고리의 다른 글
JAVA 4일차 - 객체 지향 프로그래밍(2) (0) | 2022.06.23 |
---|---|
JAVA 4일차 - 객체 지향 프로그래밍(1) (0) | 2022.06.23 |
명품 자바 프로그래밍 에센셜 실습문제 3장 (0) | 2022.06.21 |
JAVA 2일차(2) - 배열 (0) | 2022.06.21 |
JAVA 2일차 - 반복문(for,while) (0) | 2022.06.21 |
댓글