package com.dream.array;
import java.util.Scanner;
//입력 받은 개수만큼 데이터를 입/출력하고 총합을 구하기
public class ArrayEx08 {
public static void main(String[] args) {
//1.배열의 원소의 개수를 담을 변수와 총합을 담을 변수, 입력 스트립 객체 변수를 선언한다.
Scanner scan = new Scanner(System.in);
int num=0, total=0;
//2.배열의 원소 개수를 사용자에게 입력받는다.
System.out.println("배열의 원소 개수를 입력하세요.");
System.out.print("입력: ");
num = scan.nextInt( );
//3.입력받은 숫자만큼 배열의 크기를 지정한다.
int[ ] arr = new int[num];
//4.for문을 통해 사용자로부터 숫자를 입력받는다.
for(int i=0; i<arr.length; i++) {
System.out.print((i+1)+"번의 값: ");
arr[i] = scan.nextInt( );
}
System.out.println( );
//5.배열의 원소의 값을 for문으로 출력한다.
for(int i=0; i<num; i++) {
System.out.println((i+1)+"번 배열의 원소 값 : "+arr[i]);
}
//6.for문으로 배열 원소의 값을 총합을 담을 변수에 누적시킨다.
for(int i=0; i<num; i++) {
total += arr[i];
}
//7.총합을 출력한다.
System.out.println("\n배열 원소의 총합 : " + total);
}
}