Notice
Recent Posts
Recent Comments
Link
Everything has an expiration date
037 - Java for문과 array를 이용하여 if문으로 짝수, 홀수의 합 구하는 프로그램 재구성 본문
// for문, array 사용해서 Test037 재구성
import java.util.Scanner;
public class Mytest3
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("5개의 정수 입력(공백 구분) : ");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int d = sc.nextInt();
int e = sc.nextInt();
int[ ] inputData = { a,b,c,d,e };
// ◆ 값으로 배열 생성 : String[ ] names = { “혼공자”, “혼공족장”, “자바맨” };
// ◆ new 연산자로 배열 생성 : int[] intArray = new int[5];
int evenSum = 0;
int oddSum = 0;
for (int i=0 ; i<5 ; i++)
{
if (inputData[i] % 2 == 0)
{
evenSum += inputData[i];
}
else
{
oddSum += inputData[i];
}
}
System.out.printf("\n 짝수의 합 : %d, 홀수의 합 : %d \n", evenSum, oddSum);
}
}